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":
saveStyle(request, sendResponse);
return true;
case "invalidateCache":
if (typeof invalidateCache != "undefined") {
invalidateCache(false);
}
break;
case "healthCheck":
getDatabase(function() { sendResponse(true); }, function() { sendResponse(false); });
break;

View File

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