allow non-object values in db

This commit is contained in:
tophf 2022-01-29 15:35:01 +03:00
parent c597303692
commit f966b2ef96
4 changed files with 33 additions and 13 deletions

View File

@ -1,7 +1,6 @@
/* global API msg */// msg.js
/* global addAPI bgReady */// common.js
/* global createWorker */// worker-util.js
/* global db */
/* global prefs */
/* global styleMan */
/* global syncMan */
@ -39,7 +38,6 @@ addAPI(/** @namespace API */ {
},
}))(),
drafts: db.open('drafts'),
styles: styleMan,
sync: syncMan,
updater: updateMan,
@ -184,6 +182,23 @@ chrome.runtime.onInstalled.addListener(({reason, previousVersion}) => {
require(['/background/remove-unused-storage']);
}
}
// TODO: remove this before 1.5.23 as it's only for a few users who installed git 26b75e77
if (reason === 'update' && previousVersion === '1.5.22') {
for (const dbName of ['drafts', prefs.STORAGE_KEY]) {
try {
indexedDB.open(dbName).onsuccess = async e => {
const idb = /** @type IDBDatabase */ e.target.result;
const ta = idb.objectStoreNames[0] === 'data' && idb.transaction(['data']);
if (ta && ta.objectStore('data').autoIncrement) {
ta.abort();
idb.close();
await new Promise(setTimeout);
indexedDB.deleteDatabase(dbName);
}
};
} catch (e) {}
}
}
});
msg.on((msg, sender) => {

View File

@ -18,23 +18,29 @@ const db = (() => {
)(...args);
const DB = 'stylish';
const FALLBACK = 'dbInChromeStorage';
const ID_AS_KEY = {[DB]: true};
const getStoreName = dbName => dbName === DB ? 'styles' : 'data';
const proxies = {};
const proxyHandler = {
get: ({dbName}, cmd) => (...args) => exec(dbName, cmd, ...args),
};
/** @return {IDBObjectStore | {putMany: function(items:?[]):Promise<?[]>}} */
const getProxy = (dbName = DB) => proxies[dbName] || (
proxies[dbName] = new Proxy({dbName}, proxyHandler)
/**
* @param {string} dbName
* @param {boolean} [idAsKey] - if true, only objects can be stored with a unique `id` property
* @return {IDBObjectStore | {putMany: function(items:?[]):Promise<?[]>}}
*/
const getProxy = (dbName, idAsKey) => proxies[dbName] || (
(ID_AS_KEY[dbName] = idAsKey),
(proxies[dbName] = new Proxy({dbName}, proxyHandler))
);
addAPI(/** @namespace API */ {
drafts: getProxy('drafts'),
/** Storage for big items that may exceed 8kB limit of chrome.storage.sync.
* To make an item syncable register it with uuidIndex.addCustomId. */
prefsDb: getProxy(prefs.STORAGE_KEY),
});
return {
styles: getProxy(),
open: getProxy,
styles: getProxy(DB, true),
};
async function tryUsingIndexedDB() {
@ -108,10 +114,10 @@ const db = (() => {
function create(event) {
if (event.oldVersion === 0) {
const idb = event.target.result;
idb.createObjectStore(getStoreName(idb.name), {
idb.createObjectStore(getStoreName(idb.name), ID_AS_KEY[idb.name] ? {
keyPath: 'id',
autoIncrement: true,
});
} : undefined);
}
}
})();

View File

@ -505,7 +505,7 @@ const styleMan = (() => {
}
async function init() {
const orderPromise = API.prefsDb.get(INJ_ORDER);
const orderPromise = API.prefsDb.get(orderWrap.id);
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 API.prefsDb.put(orderWrap);
await API.prefsDb.put(orderWrap, orderWrap.id);
}
if (sync) {
API.sync.putDoc(orderWrap);

View File

@ -60,10 +60,9 @@
if (!isDirty) return;
API.drafts.put({
date: Date.now(),
id: makeId(),
isUsercss: editor.isUsercss,
style: editor.getValue(true),
si: editor.makeScrollInfo(),
});
}, makeId());
}
})();