From 52c2824aa90e4fa97381bd6fb8b176ed3ceb5488 Mon Sep 17 00:00:00 2001 From: Amoghavarsha Kudaligi Date: Wed, 8 May 2024 11:49:11 +0530 Subject: [PATCH] Fix zero cookie issue. Move setInterval to new objectCreation of SynchnorousCookieStore. --- packages/extension/src/serviceWorker/index.ts | 28 ------------------- .../src/store/synchnorousCookieStore.ts | 23 +++++++++++++-- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/packages/extension/src/serviceWorker/index.ts b/packages/extension/src/serviceWorker/index.ts index eab49c673..6f22762f4 100644 --- a/packages/extension/src/serviceWorker/index.ts +++ b/packages/extension/src/serviceWorker/index.ts @@ -170,20 +170,6 @@ chrome.runtime.onStartup.addListener(async () => { syncCookieStore = new SynchnorousCookieStore(); } - // Sync cookie data between popup and Devtool. - // @todo Only send the data from the active tab and the differences. - setInterval(() => { - const data = syncCookieStore?.tabsData ?? {}; - - if (Object.keys(data).length === 0) { - return; - } - - Object.keys(data).forEach((key) => { - syncCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key)); - }); - }, 1200); - if (Object.keys(storage).includes('allowedNumberOfTabs')) { tabMode = storage.allowedNumberOfTabs; } @@ -302,20 +288,6 @@ chrome.runtime.onInstalled.addListener(async (details) => { syncCookieStore = new SynchnorousCookieStore(); syncCookieStore?.clear(); - // @see https://developer.chrome.com/blog/longer-esw-lifetimes#whats_changed - // Doing this to keep the service worker alive so that we dont loose any data and introduce any bug. - - // @todo Send tab data of the active tab only, also if sending only the difference would make it any faster. - setInterval(() => { - if (Object.keys(syncCookieStore?.tabsData ?? {}).length === 0) { - return; - } - - Object.keys(syncCookieStore?.tabsData ?? {}).forEach((key) => { - syncCookieStore?.sendUpdatedDataToPopupAndDevTools(Number(key)); - }); - }, 1200); - if (details.reason === 'install') { await chrome.storage.sync.clear(); await chrome.storage.sync.set({ diff --git a/packages/extension/src/store/synchnorousCookieStore.ts b/packages/extension/src/store/synchnorousCookieStore.ts index 97588ac3f..ae2fa595f 100644 --- a/packages/extension/src/store/synchnorousCookieStore.ts +++ b/packages/extension/src/store/synchnorousCookieStore.ts @@ -53,6 +53,22 @@ class SynchnorousCookieStore { }; } = {}; + constructor() { + // Sync cookie data between popup and Devtool. + // @todo Only send the data from the active tab and the differences. + setInterval(() => { + const data = this?.tabsData ?? {}; + + if (Object.keys(data).length === 0) { + return; + } + + Object.keys(data).forEach((key) => { + this?.sendUpdatedDataToPopupAndDevTools(Number(key)); + }); + }, 1200); + } + /** * Update cookie store. * @param {number} tabId Tab id. @@ -351,9 +367,10 @@ class SynchnorousCookieStore { try { if ( - this.tabs[tabId].devToolsOpenState || - (this.tabs[tabId].popupOpenState && - (overrideForInitialSync || this.tabs[tabId].newUpdates > 0)) + ((this.tabs[tabId].devToolsOpenState || + this.tabs[tabId].popupOpenState) && + this.tabs[tabId].newUpdates > 0) || + overrideForInitialSync ) { sentMessageAnyWhere = true;