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
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

View File

@ -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});
},
});

View File

@ -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);