1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 00:07:58 +01:00

cache.jsm magic.

This commit is contained in:
Kris Maglione
2011-09-30 02:53:35 -04:00
parent 39e8bf7a06
commit a15d2992a9

View File

@@ -22,18 +22,20 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
update(services["dactyl:"].providers, { update(services["dactyl:"].providers, {
"cache": function (uri, path) { "cache": function (uri, path) {
// TODO: Return zip reader stream let contentType = "text/plain";
let result = cache.force(path);
if (isArray(result))
return result;
try { try {
return [services.mime.getTypeFromURI(uri), result]; contentType = services.mime.getTypeFromURI(uri)
}
catch (e) {
return ["text/plain", result];
} }
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 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, compression: 9,
cacheFile: Class.Memoize(function () { cacheFile: Class.Memoize(function () {
@@ -71,7 +85,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
return this._cacheReader; return this._cacheReader;
}, },
get inQueue() this._cacheWriter && this._cacheWriter.inQueue(), get inQueue() this._cacheWriter && this._cacheWriter.inQueue,
getCacheWriter: function () { getCacheWriter: function () {
if (!this._cacheWriter) if (!this._cacheWriter)
@@ -114,7 +128,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
flush: function flush() { flush: function flush() {
cache.cache = {}; cache.cache = {};
if (this.cacheFile.eists()) { if (this.cacheFile.exists()) {
this.closeReader(); this.closeReader();
this.flushJAR(this.cacheFile); this.flushJAR(this.cacheFile);
@@ -148,8 +162,10 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
}, },
force: function force(name, localOnly) { force: function force(name, localOnly) {
util.waitFor(function () !this.inQueue, this);
if (this.cacheReader && this.cacheReader.hasEntry(name)) { if (this.cacheReader && this.cacheReader.hasEntry(name)) {
return JSON.parse(File.readStream( return this.parse(File.readStream(
this.cacheReader.getInputStream(name))); this.cacheReader.getInputStream(name)));
} }
@@ -207,7 +223,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
this.queue.splice(0).forEach(function ([time, entry]) { this.queue.splice(0).forEach(function ([time, entry]) {
if (time && Set.has(this.cache, entry)) { if (time && Set.has(this.cache, entry)) {
let stream = services.CharsetConv("UTF-8") 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.getCacheWriter().addEntryStream(entry, time * 1000,
this.compression, stream, this.compression, stream,