diff --git a/background/background.js b/background/background.js index 95679239..1d5b8605 100644 --- a/background/background.js +++ b/background/background.js @@ -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) => { diff --git a/background/db.js b/background/db.js index 4c5f112a..c14e8796 100644 --- a/background/db.js +++ b/background/db.js @@ -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); } } })(); diff --git a/background/style-manager.js b/background/style-manager.js index b846ec3d..8a147f9f 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -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); diff --git a/edit/drafts.js b/edit/drafts.js index f66e261a..7a3b44d2 100644 --- a/edit/drafts.js +++ b/edit/drafts.js @@ -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()); } })();