diff --git a/background/background.js b/background/background.js index 95679239..24631a52 100644 --- a/background/background.js +++ b/background/background.js @@ -155,6 +155,12 @@ addAPI(/** @namespace API */ { getValues: () => prefs.__values, // will be deepCopy'd by apiHandler set: prefs.set, }, + + /** + * Storage for big items that may exceed 8kB limit of chrome.storage.sync. + * To make an item syncable register it with uuidIndex.addCustomId. + */ + prefsDb: db.open(prefs.STORAGE_KEY), }); //#endregion diff --git a/background/common.js b/background/common.js index 9eb6ba1e..ef1c0ffd 100644 --- a/background/common.js +++ b/background/common.js @@ -11,11 +11,9 @@ bgReady.all = new Promise(r => (bgReady._resolveAll = r)); const uuidIndex = Object.assign(new Map(), { custom: {}, - addCustomId(obj, setter) { - Object.defineProperty(uuidIndex.custom, obj.id, { - get: () => obj, - set: setter, - }); + /** `obj` must have a unique `id`, a UUIDv4 `_id`, and Date.now() for `_rev`. */ + addCustomId(obj, {get = () => obj, set}) { + Object.defineProperty(uuidIndex.custom, obj.id, {get, set}); }, }); diff --git a/background/style-manager.js b/background/style-manager.js index 3d4f4d9a..b976ef20 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -73,7 +73,7 @@ const styleMan = (() => { _id: `${chrome.runtime.id}-${INJ_ORDER}`, _rev: 0, }; - uuidIndex.addCustomId(orderWrap, setOrder); + uuidIndex.addCustomId(orderWrap, {set: setOrder}); /** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */ let ready = init(); @@ -505,7 +505,7 @@ const styleMan = (() => { } async function init() { - const orderPromise = db.open(prefs.STORAGE_KEY).get(INJ_ORDER); + const orderPromise = API.prefsDb.get(INJ_ORDER); const styles = await db.styles.getAll() || []; const updated = await Promise.all(styles.map(fixKnownProblems).filter(Boolean)); if (updated.length) { @@ -738,7 +738,7 @@ const styleMan = (() => { msg.broadcast({method: 'styleSort', order}); } if (store) { - await db.open(prefs.STORAGE_KEY).put(orderWrap); + await API.prefsDb.put(orderWrap); } if (sync) { API.sync.putDoc(orderWrap);