From f256f558dc4e4235cd62a527282d09318ddf3d6f Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 19 Mar 2017 22:49:43 +0300 Subject: [PATCH] IndexedDB getAll to read all styles in one op Available since Chrome 48, FF44 (or FF27+ using dom.indexedDB.experimental flag) --- storage.js | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/storage.js b/storage.js index 8a40ec21..5c627d18 100644 --- a/storage.js +++ b/storage.js @@ -20,7 +20,7 @@ function getDatabase(ready, error) { // Let manage/popup/edit reuse background page variables // Note, only "var"-declared variables are visible from another extension page -var cachedStyles = ((bg) => bg && bg.cache || { +var cachedStyles = ((bg) => bg && bg.cachedStyles || { bg, list: null, noCode: null, @@ -67,35 +67,27 @@ function getStyles(options, callback) { getDatabase(db => { const tx = db.transaction(['styles'], 'readonly'); const os = tx.objectStore('styles'); - const all = []; - os.openCursor().onsuccess = event => { - const cursor = event.target.result; - if (cursor) { - const s = cursor.value; - s.id = cursor.key; - all.push(cursor.value); - cursor.continue(); - } else { - cachedStyles.list = all; - cachedStyles.noCode = []; - for (let style of all) { - const noCode = getStyleWithNoCode(style); - cachedStyles.noCode.push(noCode); - cachedStyles.byId.set(style.id, {style, noCode}); - } - //console.log('%s getStyles %s, invoking cached callbacks: %o', (performance.now() - t0).toFixed(1), JSON.stringify(options), cache.mutex.onDone.map(e => JSON.stringify(e.options))) - try{ - callback(filterStyles(options)); - } catch(e){ - // no error in console, it works - } - - cachedStyles.mutex.inProgress = false; - for (let {options, callback} of cachedStyles.mutex.onDone) { - callback(filterStyles(options)); - } - cachedStyles.mutex.onDone = []; + os.getAll().onsuccess = event => { + cachedStyles.list = event.target.result || []; + cachedStyles.noCode = []; + cachedStyles.byId.clear(); + for (let style of cachedStyles.list) { + const noCode = getStyleWithNoCode(style); + cachedStyles.noCode.push(noCode); + cachedStyles.byId.set(style.id, {style, noCode}); } + //console.log('%s getStyles %s, invoking cached callbacks: %o', (performance.now() - t0).toFixed(1), JSON.stringify(options), cachedStyles.mutex.onDone.map(e => JSON.stringify(e.options))) + try{ + callback(filterStyles(options)); + } catch(e){ + // no error in console, it works + } + + cachedStyles.mutex.inProgress = false; + for (let {options, callback} of cachedStyles.mutex.onDone) { + callback(filterStyles(options)); + } + cachedStyles.mutex.onDone = []; }; }, null); }