summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/Storage.js44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/common/Storage.js b/src/common/Storage.js
index 4555a28..0e8181e 100644
--- a/src/common/Storage.js
+++ b/src/common/Storage.js
@@ -1,16 +1,47 @@
var Storage = {
async safeOp(op, type, keys) {
+ let sync = type === "sync";
+ if (sync && op === "get") {
+ let localFallback = await this.localFallback();
+ if (localFallback.size) {
+ for (let k of Array.isArray(keys) ? keys : [keys]) {
+ if (localFallback.has(k)) {
+ type = "local";
+ break;
+ }
+ }
+ }
+ }
try {
- return await browser.storage[type][op](keys);
+ let ret = await browser.storage[type][op](keys);
+ if (sync && op === "set") {
+ let localFallback = await this.localFallback();
+ let size = localFallback.size;
+ if (size > 0) {
+ for (let k of Object.keys(keys)) {
+ localFallback.delete(k);
+ }
+ if (size > localFallback.size) this.localFallback(localFallback);
+ }
+ }
+ return ret;
} catch (e) {
- if (type === "sync") {
+ if (sync) {
debug("Sync disabled? Falling back to local storage (%s %o)", op, keys);
+ let localFallback = await this.localFallback();
+ let failedKeys = Array.isArray(keys) ? keys
+ : typeof keys === "string" ? [keys] : Object.keys(keys);
+ for (let k of failedKeys) {
+ localFallback.add(k);
+ }
+ await this.localFallback(localFallback);
} else {
error(e);
throw e;
}
}
+
return await browser.storage.local[op](keys);
},
@@ -20,5 +51,14 @@ var Storage = {
async set(type, keys) {
return await this.safeOp("set", type, keys);
+ },
+
+ async localFallback(keys) {
+ let name = "__fallbackKeys";
+ if (keys) {
+ return await browser.storage.local.set({[name]: [...keys]});
+ }
+ let fallbackKeys = (await browser.storage.local.get(name))[name];
+ return new Set(Array.isArray(fallbackKeys) ? fallbackKeys : []);
}
}