Bring back caching styles in memory

This commit is contained in:
Jason 2016-03-19 18:27:28 -05:00
parent ca8324ab67
commit 380bfc6ed7
2 changed files with 21 additions and 0 deletions

View File

@ -66,6 +66,11 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
case "saveStyle": case "saveStyle":
saveStyle(request, sendResponse); saveStyle(request, sendResponse);
return true; return true;
case "invalidateCache":
if (typeof invalidateCache != "undefined") {
invalidateCache(false);
}
break;
case "healthCheck": case "healthCheck":
getDatabase(function() { sendResponse(true); }, function() { sendResponse(false); }); getDatabase(function() { sendResponse(true); }, function() { sendResponse(false); });
break; break;

View File

@ -17,7 +17,12 @@ function getDatabase(ready, error) {
} }
}; };
var cachedStyles = null;
function getStyles(options, callback) { function getStyles(options, callback) {
if (cachedStyles != null) {
callback(filterStyles(cachedStyles, options));
return;
}
getDatabase(function(db) { getDatabase(function(db) {
var tx = db.transaction(["styles"], "readonly"); var tx = db.transaction(["styles"], "readonly");
var os = tx.objectStore("styles"); var os = tx.objectStore("styles");
@ -30,12 +35,20 @@ function getStyles(options, callback) {
all.push(cursor.value); all.push(cursor.value);
cursor.continue(); cursor.continue();
} else { } else {
cachedStyles = all;
callback(filterStyles(all, options)); callback(filterStyles(all, options));
} }
}; };
}, null); }, null);
} }
function invalidateCache(andNotify) {
cachedStyles = null;
if (andNotify) {
chrome.runtime.sendMessage({method: "invalidateCache"});
}
}
function filterStyles(styles, options) { function filterStyles(styles, options) {
var enabled = fixBoolean(options.enabled); var enabled = fixBoolean(options.enabled);
var url = "url" in options ? options.url : null; var url = "url" in options ? options.url : null;
@ -97,6 +110,7 @@ function saveStyle(o, callback) {
request = os.put(style); request = os.put(style);
request.onsuccess = function(event) { request.onsuccess = function(event) {
notifyAllTabs({method: "styleUpdated", style: style}); notifyAllTabs({method: "styleUpdated", style: style});
invalidateCache(true);
if (callback) { if (callback) {
callback(style); callback(style);
} }
@ -128,6 +142,7 @@ function saveStyle(o, callback) {
delete o["id"]; delete o["id"];
var request = os.add(o); var request = os.add(o);
request.onsuccess = function(event) { request.onsuccess = function(event) {
invalidateCache(true);
// Give it the ID that was generated // Give it the ID that was generated
o.id = event.target.result; o.id = event.target.result;
notifyAllTabs({method: "styleAdded", style: o}); notifyAllTabs({method: "styleAdded", style: o});
@ -152,6 +167,7 @@ function deleteStyle(id) {
var request = os.delete(Number(id)); var request = os.delete(Number(id));
request.onsuccess = function(event) { request.onsuccess = function(event) {
handleDelete(id); handleDelete(id);
invalidateCache(true);
notifyAllTabs({method: "styleDeleted", id: id}); notifyAllTabs({method: "styleDeleted", id: id});
}; };
}); });