From 329d0caac1124b8bab2d08f606eb9dc41e0a49b5 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 23 Feb 2022 09:07:30 +0300 Subject: [PATCH] avoid flicker/delay when opening manager --- background/background.js | 1 + background/db.js | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/background/background.js b/background/background.js index c3880247..c2fc7898 100644 --- a/background/background.js +++ b/background/background.js @@ -119,6 +119,7 @@ addAPI(/** @namespace API */ { } 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 }, diff --git a/background/db.js b/background/db.js index c14e8796..f674723a 100644 --- a/background/db.js +++ b/background/db.js @@ -1,6 +1,7 @@ /* global addAPI */// common.js /* global chromeLocal */// storage-util.js /* global cloneError */// worker-util.js +/* global deepCopy */// toolbox.js /* global prefs */ 'use strict'; @@ -20,9 +21,12 @@ const db = (() => { const FALLBACK = 'dbInChromeStorage'; const ID_AS_KEY = {[DB]: true}; const getStoreName = dbName => dbName === DB ? 'styles' : 'data'; + const cache = {}; const proxies = {}; const proxyHandler = { - get: ({dbName}, cmd) => (...args) => exec(dbName, cmd, ...args), + get: ({dbName}, cmd) => + (...args) => + (dbName === DB ? exec : cachedExec)(dbName, cmd, ...args), }; /** * @param {string} dbName @@ -43,6 +47,19 @@ const db = (() => { 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() { // 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