diff options
author | hackademix | 2019-10-05 10:56:00 +0200 |
---|---|---|
committer | hackademix | 2019-10-05 10:59:07 +0200 |
commit | c2ea02fd24c9f0775fd5ff51a58df38078060e87 (patch) | |
tree | fba94f19a7fae5dbb82c48a6599520eee3f6d7f3 /src/common | |
parent | 7cc3422b4f5ead98ad6b11cd66c18ce54c066101 (diff) | |
download | noscript-c2ea02fd24c9f0775fd5ff51a58df38078060e87.tar.gz noscript-c2ea02fd24c9f0775fd5ff51a58df38078060e87.tar.xz noscript-c2ea02fd24c9f0775fd5ff51a58df38078060e87.zip |
Fallback to local storage for any item exceeding limits (fixes persistence problems on Chromium).
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Storage.js | 44 |
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 : []); } } |