From c2ea02fd24c9f0775fd5ff51a58df38078060e87 Mon Sep 17 00:00:00 2001 From: hackademix Date: Sat, 5 Oct 2019 10:56:00 +0200 Subject: Fallback to local storage for any item exceeding limits (fixes persistence problems on Chromium). --- src/common/Storage.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/common') 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 : []); } } -- cgit v1.2.3