diff --git a/webextensions/background/background-cache.js b/webextensions/background/background-cache.js index cc2882124..279b8c5a7 100644 --- a/webextensions/background/background-cache.js +++ b/webextensions/background/background-cache.js @@ -517,10 +517,27 @@ Tab.onHidden.addListener(tab => { reserveToCacheTree(tab.windowId); }); +browser.runtime.onMessage.addListener((message, _sender) => { + switch (message && message.type) { + case Constants.kCOMMAND_GET_ON_MEMORY_CACHE: + return mCaches[message.key]; + + case Constants.kCOMMAND_SET_ON_MEMORY_CACHE: + if (message.value) + mCaches[message.key] = message.value; + else + delete mCaches[message.key]; + return; + + default: + return; + } +}); + browser.windows.onRemoved.addListener(async windowId => { - const storageKeyPrefix = `backgroundCache-${await UniqueId.ensureWindowId(windowId)}-`; + const storageKeyPart = `Cache-${await UniqueId.ensureWindowId(windowId)}-`; for (const key in mCaches) { - if (key.startsWith(storageKeyPrefix)) + if (key.includes(storageKeyPart)) delete mCaches[key]; } }); diff --git a/webextensions/common/constants.js b/webextensions/common/constants.js index 4f2ef1de6..9ca9647d4 100644 --- a/webextensions/common/constants.js +++ b/webextensions/common/constants.js @@ -92,6 +92,9 @@ export const kCOMMAND_UNBLOCK_USER_OPERATIONS = 'treestyletab:unblock-user-opera export const kCOMMAND_PROGRESS_USER_OPERATIONS = 'treestyletab:progress-user-operations'; export const kCOMMAND_BROADCAST_TAB_STATE = 'treestyletab:broadcast-tab-state'; +export const kCOMMAND_SET_ON_MEMORY_CACHE = 'treestyletab:set-on-memory-cache'; +export const kCOMMAND_GET_ON_MEMORY_CACHE = 'treestyletab:get-on-memory-cache'; + export const kCOMMAND_BOOKMARK_TAB_WITH_DIALOG = 'treestyletab:bookmark-tab-with-dialog'; export const kCOMMAND_BOOKMARK_TABS_WITH_DIALOG = 'treestyletab:bookmark-tabs-with-dialog'; diff --git a/webextensions/sidebar/sidebar-cache.js b/webextensions/sidebar/sidebar-cache.js index f5ab172a4..53cea5488 100644 --- a/webextensions/sidebar/sidebar-cache.js +++ b/webextensions/sidebar/sidebar-cache.js @@ -437,17 +437,13 @@ async function fixupTabsRestoredFromCache(tabElements, tabs, options = {}) { // =================================================================== async function updateWindowCache(key, value) { - if (!configs.persistCachedTree && - browser.storage.session) { + if (!configs.persistCachedTree) { const storagKey = `sidebarCache-${await UniqueId.ensureWindowId(mTargetWindow)}-${key}`; - if (value) { - const data = {}; - data[storagKey] = value; - browser.storage.session.set(data); - } - else { - browser.storage.session.remove(storagKey); - } + browser.runtime.sendMessage({ + type: Constants.kCOMMAND_SET_ON_MEMORY_CACHE, + key: storagKey, + value, + }); return; } @@ -495,10 +491,9 @@ export function markWindowCacheDirty(key) { async function getWindowCache(key) { if (!configs.persistCachedTree) { const storageKey = `sidebarCache-${await UniqueId.ensureWindowId(mTargetWindow)}-${key}`; - const defaultData = {}; - defaultData[storageKey] = undefined; - return browser.storage.session.get(defaultData).then(data => { - return data[storageKey]; + return browser.runtime.sendMessage({ + type: Constants.kCOMMAND_GET_ON_MEMORY_CACHE, + key: storageKey, }); }