API.prefsDb

This commit is contained in:
tophf 2022-01-28 19:22:14 +03:00
parent 2e7f7d0f62
commit bba0c9d7a1
3 changed files with 12 additions and 8 deletions

View File

@ -155,6 +155,12 @@ addAPI(/** @namespace API */ {
getValues: () => prefs.__values, // will be deepCopy'd by apiHandler getValues: () => prefs.__values, // will be deepCopy'd by apiHandler
set: prefs.set, 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 //#endregion

View File

@ -11,11 +11,9 @@ bgReady.all = new Promise(r => (bgReady._resolveAll = r));
const uuidIndex = Object.assign(new Map(), { const uuidIndex = Object.assign(new Map(), {
custom: {}, custom: {},
addCustomId(obj, setter) { /** `obj` must have a unique `id`, a UUIDv4 `_id`, and Date.now() for `_rev`. */
Object.defineProperty(uuidIndex.custom, obj.id, { addCustomId(obj, {get = () => obj, set}) {
get: () => obj, Object.defineProperty(uuidIndex.custom, obj.id, {get, set});
set: setter,
});
}, },
}); });

View File

@ -73,7 +73,7 @@ const styleMan = (() => {
_id: `${chrome.runtime.id}-${INJ_ORDER}`, _id: `${chrome.runtime.id}-${INJ_ORDER}`,
_rev: 0, _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` */ /** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */
let ready = init(); let ready = init();
@ -505,7 +505,7 @@ const styleMan = (() => {
} }
async function init() { 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 styles = await db.styles.getAll() || [];
const updated = await Promise.all(styles.map(fixKnownProblems).filter(Boolean)); const updated = await Promise.all(styles.map(fixKnownProblems).filter(Boolean));
if (updated.length) { if (updated.length) {
@ -738,7 +738,7 @@ const styleMan = (() => {
msg.broadcast({method: 'styleSort', order}); msg.broadcast({method: 'styleSort', order});
} }
if (store) { if (store) {
await db.open(prefs.STORAGE_KEY).put(orderWrap); await API.prefsDb.put(orderWrap);
} }
if (sync) { if (sync) {
API.sync.putDoc(orderWrap); API.sync.putDoc(orderWrap);