diff options
author | Kris Maglione <maglione.k@gmail.com> | 2011-09-30 02:53:35 -0400 |
---|---|---|
committer | Kris Maglione <maglione.k@gmail.com> | 2011-09-30 02:53:35 -0400 |
commit | a15d2992a93876e1576a8ab31f9e547059e2661e (patch) | |
tree | dcf8a583346f22dc5d3559951e1301336e1c63ab /common | |
parent | 39e8bf7a06a25f3a0da0200fdd142df97fb9b2cb (diff) | |
download | pentadactyl-a15d2992a93876e1576a8ab31f9e547059e2661e.tar.gz |
cache.jsm magic.
Diffstat (limited to 'common')
-rw-r--r-- | common/modules/cache.jsm | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/common/modules/cache.jsm b/common/modules/cache.jsm index 72556bc7..6bc5faa6 100644 --- a/common/modules/cache.jsm +++ b/common/modules/cache.jsm @@ -22,18 +22,20 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), { update(services["dactyl:"].providers, { "cache": function (uri, path) { - // TODO: Return zip reader stream - - let result = cache.force(path); - if (isArray(result)) - return result; - + let contentType = "text/plain"; try { - return [services.mime.getTypeFromURI(uri), result]; - } - catch (e) { - return ["text/plain", result]; + contentType = services.mime.getTypeFromURI(uri) } + catch (e) {} + + if (!cache.cacheReader || !cache.cacheReader.hasEntry(path)) + return [contentType, cache.force(path)]; + + let channel = services.StreamChannel(uri); + channel.contentStream = cache.cacheReader.getInputStream(path); + channel.contentType = contentType; + channel.contentCharset = "UTF-8"; + return channel; } }); }, @@ -47,6 +49,18 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), { isLocal: true }), + parse: function parse(str) { + if (~'{['.indexOf(str[0])) + return JSON.parse(str); + return str; + }, + + stringify: function stringify(obj) { + if (isString(obj)) + return obj; + return JSON.stringify(obj); + }, + compression: 9, cacheFile: Class.Memoize(function () { @@ -71,7 +85,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), { return this._cacheReader; }, - get inQueue() this._cacheWriter && this._cacheWriter.inQueue(), + get inQueue() this._cacheWriter && this._cacheWriter.inQueue, getCacheWriter: function () { if (!this._cacheWriter) @@ -114,7 +128,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), { flush: function flush() { cache.cache = {}; - if (this.cacheFile.eists()) { + if (this.cacheFile.exists()) { this.closeReader(); this.flushJAR(this.cacheFile); @@ -148,8 +162,10 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), { }, force: function force(name, localOnly) { + util.waitFor(function () !this.inQueue, this); + if (this.cacheReader && this.cacheReader.hasEntry(name)) { - return JSON.parse(File.readStream( + return this.parse(File.readStream( this.cacheReader.getInputStream(name))); } @@ -207,7 +223,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), { this.queue.splice(0).forEach(function ([time, entry]) { if (time && Set.has(this.cache, entry)) { let stream = services.CharsetConv("UTF-8") - .convertToInputStream(JSON.stringify(this.cache[entry])); + .convertToInputStream(this.stringify(this.cache[entry])); this.getCacheWriter().addEntryStream(entry, time * 1000, this.compression, stream, |