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 API msg */// msg.js
/* global addAPI bgReady */// common.js /* global addAPI bgReady */// common.js
/* global createWorker */// worker-util.js /* global createWorker */// worker-util.js
/* global db */
/* global prefs */ /* global prefs */
/* global styleMan */ /* global styleMan */
/* global syncMan */ /* global syncMan */
@ -39,7 +38,6 @@ addAPI(/** @namespace API */ {
}, },
}))(), }))(),
drafts: db.open('drafts'),
styles: styleMan, styles: styleMan,
sync: syncMan, sync: syncMan,
updater: updateMan, updater: updateMan,
@ -184,6 +182,23 @@ chrome.runtime.onInstalled.addListener(({reason, previousVersion}) => {
require(['/background/remove-unused-storage']); 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) => { msg.on((msg, sender) => {

View File

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

View File

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

View File

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