Skip to content

Commit

Permalink
Don't use storage.session for sidebar cache #3434
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jan 23, 2024
1 parent 85dd951 commit 1eb00fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
21 changes: 19 additions & 2 deletions webextensions/background/background-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
});
Expand Down
3 changes: 3 additions & 0 deletions webextensions/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
23 changes: 9 additions & 14 deletions webextensions/sidebar/sidebar-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
});
}

Expand Down

0 comments on commit 1eb00fa

Please sign in to comment.