IndexedDB getAll to read all styles in one op

Available since Chrome 48, FF44 (or FF27+ using dom.indexedDB.experimental flag)
This commit is contained in:
tophf 2017-03-19 22:49:43 +03:00
parent f4e689721a
commit f256f558dc

View File

@ -20,7 +20,7 @@ function getDatabase(ready, error) {
// Let manage/popup/edit reuse background page variables // Let manage/popup/edit reuse background page variables
// Note, only "var"-declared variables are visible from another extension page // Note, only "var"-declared variables are visible from another extension page
var cachedStyles = ((bg) => bg && bg.cache || { var cachedStyles = ((bg) => bg && bg.cachedStyles || {
bg, bg,
list: null, list: null,
noCode: null, noCode: null,
@ -67,23 +67,16 @@ function getStyles(options, callback) {
getDatabase(db => { getDatabase(db => {
const tx = db.transaction(['styles'], 'readonly'); const tx = db.transaction(['styles'], 'readonly');
const os = tx.objectStore('styles'); const os = tx.objectStore('styles');
const all = []; os.getAll().onsuccess = event => {
os.openCursor().onsuccess = event => { cachedStyles.list = event.target.result || [];
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 = []; cachedStyles.noCode = [];
for (let style of all) { cachedStyles.byId.clear();
for (let style of cachedStyles.list) {
const noCode = getStyleWithNoCode(style); const noCode = getStyleWithNoCode(style);
cachedStyles.noCode.push(noCode); cachedStyles.noCode.push(noCode);
cachedStyles.byId.set(style.id, {style, 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))) //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{ try{
callback(filterStyles(options)); callback(filterStyles(options));
} catch(e){ } catch(e){
@ -95,7 +88,6 @@ function getStyles(options, callback) {
callback(filterStyles(options)); callback(filterStyles(options));
} }
cachedStyles.mutex.onDone = []; cachedStyles.mutex.onDone = [];
}
}; };
}, null); }, null);
} }