avoid flicker/delay when opening manager

This commit is contained in:
tophf 2022-02-23 09:07:30 +03:00
parent 51e9b09f52
commit 329d0caac1
2 changed files with 19 additions and 1 deletions

View File

@ -119,6 +119,7 @@ addAPI(/** @namespace API */ {
} }
return tab; return tab;
} }
API.prefsDb.get('badFavs'); // prime the cache to avoid flicker/delay when opening the page
return openURL({url, ignoreExisting: true}).then(activateTab); // activateTab unminimizes the window return openURL({url, ignoreExisting: true}).then(activateTab); // activateTab unminimizes the window
}, },

View File

@ -1,6 +1,7 @@
/* global addAPI */// common.js /* global addAPI */// common.js
/* global chromeLocal */// storage-util.js /* global chromeLocal */// storage-util.js
/* global cloneError */// worker-util.js /* global cloneError */// worker-util.js
/* global deepCopy */// toolbox.js
/* global prefs */ /* global prefs */
'use strict'; 'use strict';
@ -20,9 +21,12 @@ const db = (() => {
const FALLBACK = 'dbInChromeStorage'; const FALLBACK = 'dbInChromeStorage';
const ID_AS_KEY = {[DB]: true}; const ID_AS_KEY = {[DB]: true};
const getStoreName = dbName => dbName === DB ? 'styles' : 'data'; const getStoreName = dbName => dbName === DB ? 'styles' : 'data';
const cache = {};
const proxies = {}; const proxies = {};
const proxyHandler = { const proxyHandler = {
get: ({dbName}, cmd) => (...args) => exec(dbName, cmd, ...args), get: ({dbName}, cmd) =>
(...args) =>
(dbName === DB ? exec : cachedExec)(dbName, cmd, ...args),
}; };
/** /**
* @param {string} dbName * @param {string} dbName
@ -43,6 +47,19 @@ const db = (() => {
styles: getProxy(DB, true), styles: getProxy(DB, true),
}; };
async function cachedExec(dbName, cmd, a, b) {
const hub = cache[dbName] || (cache[dbName] = {});
const res = cmd === 'get' && a in hub ? hub[a] : await exec(...arguments);
if (cmd === 'get') {
hub[a] = deepCopy(res);
} else if (cmd === 'put') {
hub[ID_AS_KEY[dbName] ? a.id : b] = deepCopy(a);
} else if (cmd === 'delete') {
delete hub[a];
}
return res;
}
async function tryUsingIndexedDB() { async function tryUsingIndexedDB() {
// we use chrome.storage.local fallback if IndexedDB doesn't save data, // we use chrome.storage.local fallback if IndexedDB doesn't save data,
// which, once detected on the first run, is remembered in chrome.storage.local // which, once detected on the first run, is remembered in chrome.storage.local