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,35 +67,27 @@ 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; cachedStyles.noCode = [];
if (cursor) { cachedStyles.byId.clear();
const s = cursor.value; for (let style of cachedStyles.list) {
s.id = cursor.key; const noCode = getStyleWithNoCode(style);
all.push(cursor.value); cachedStyles.noCode.push(noCode);
cursor.continue(); cachedStyles.byId.set(style.id, {style, noCode});
} 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 = [];
} }
//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); }, null);
} }