From e64b65b5111eb513d2cd93e39c3f84aa07eca9d8 Mon Sep 17 00:00:00 2001 From: mbnuqw Date: Sat, 6 Apr 2024 20:31:59 +0500 Subject: [PATCH] fix: tabs: incorrect state checking in event handlers (resolves #1230) --- src/services/tabs.fg.actions.ts | 15 ++++++++------- src/services/tabs.fg.handlers.ts | 14 +++++++------- src/services/tabs.fg.shadow.ts | 17 ++++++++++------- src/services/tabs.fg.ts | 1 + 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/services/tabs.fg.actions.ts b/src/services/tabs.fg.actions.ts index 96092ce8a..fe40484cf 100644 --- a/src/services/tabs.fg.actions.ts +++ b/src/services/tabs.fg.actions.ts @@ -201,6 +201,14 @@ export async function load(): Promise { if (Settings.state.colorizeTabsBranches) Tabs.colorizeBranches() Tabs.ready = true + + // Call deferred event handlers + if (Tabs.deferredEventHandling.length) { + Logs.warn('Tabs: Deferred event handlers:', Tabs.deferredEventHandling.length) + } + Tabs.deferredEventHandling.forEach(cb => cb()) + Tabs.deferredEventHandling = [] + waitingForTabs.forEach(cb => cb()) waitingForTabs = [] @@ -338,13 +346,6 @@ async function restoreTabsState(): Promise { Tabs.updateSuccessionDebounced(0) } - // Call deferred event handlers - if (Tabs.deferredEventHandling.length) { - Logs.warn('Tabs: Deferred event handlers:', Tabs.deferredEventHandling.length) - } - Tabs.deferredEventHandling.forEach(cb => cb()) - Tabs.deferredEventHandling = [] - Logs.info(`Tabs.restoreTabsState: Done: ${performance.now() - ts}ms`) } diff --git a/src/services/tabs.fg.handlers.ts b/src/services/tabs.fg.handlers.ts index a61a08368..ca8c118c5 100644 --- a/src/services/tabs.fg.handlers.ts +++ b/src/services/tabs.fg.handlers.ts @@ -98,7 +98,7 @@ function releaseReopenedTabsBuffer(): void { function onTabCreated(nativeTab: NativeTab, attached?: boolean): void { if (nativeTab.windowId !== Windows.id) return - if (Tabs.list.length === 0 || Tabs.sorting) { + if (!Tabs.ready || Tabs.sorting) { Tabs.deferredEventHandling.push(() => onTabCreated(nativeTab)) return } @@ -460,7 +460,7 @@ function onTabCreated(nativeTab: NativeTab, attached?: boolean): void { */ function onTabUpdated(tabId: ID, change: browser.tabs.ChangeInfo, nativeTab: NativeTab): void { if (nativeTab.windowId !== Windows.id) return - if (Tabs.list.length === 0 || waitForOtherReopenedTabsBuffer || Tabs.sorting) { + if (!Tabs.ready || waitForOtherReopenedTabsBuffer || Tabs.sorting) { Tabs.deferredEventHandling.push(() => onTabUpdated(tabId, change, nativeTab)) return } @@ -823,7 +823,7 @@ function rememberChildTabs(childId: ID, parentId: ID): void { */ function onTabRemoved(tabId: ID, info: browser.tabs.RemoveInfo, detached?: boolean): void { if (info.windowId !== Windows.id) return - if (Tabs.list.length === 0 || waitForOtherReopenedTabsBuffer || Tabs.sorting) { + if (!Tabs.ready || waitForOtherReopenedTabsBuffer || Tabs.sorting) { Tabs.deferredEventHandling.push(() => onTabRemoved(tabId, info, detached)) return } @@ -1118,7 +1118,7 @@ function onTabRemoved(tabId: ID, info: browser.tabs.RemoveInfo, detached?: boole */ function onTabMoved(id: ID, info: browser.tabs.MoveInfo): void { if (info.windowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.ready) { Tabs.deferredEventHandling.push(() => onTabMoved(id, info)) return } @@ -1245,7 +1245,7 @@ function onTabMoved(id: ID, info: browser.tabs.MoveInfo): void { */ function onTabDetached(id: ID, info: browser.tabs.DetachInfo): void { if (info.oldWindowId !== Windows.id) return - if (Tabs.list.length === 0 || Tabs.sorting) { + if (!Tabs.ready || Tabs.sorting) { Tabs.deferredEventHandling.push(() => onTabDetached(id, info)) return } @@ -1276,7 +1276,7 @@ function onTabDetached(id: ID, info: browser.tabs.DetachInfo): void { const deferredActivationHandling = { id: NOID, cb: null as (() => void) | null } async function onTabAttached(id: ID, info: browser.tabs.AttachInfo): Promise { if (info.newWindowId !== Windows.id) return - if (Tabs.list.length === 0 || Tabs.sorting) { + if (!Tabs.ready || Tabs.sorting) { Tabs.deferredEventHandling.push(() => onTabAttached(id, info)) return } @@ -1325,7 +1325,7 @@ let bufTabActivatedEventIndex = -1 */ function onTabActivated(info: browser.tabs.ActiveInfo): void { if (info.windowId !== Windows.id) return - if (Tabs.list.length === 0 || waitForOtherReopenedTabsBuffer) { + if (!Tabs.ready || waitForOtherReopenedTabsBuffer) { if (bufTabActivatedEventIndex !== -1) { Tabs.deferredEventHandling.splice(bufTabActivatedEventIndex, 1) } diff --git a/src/services/tabs.fg.shadow.ts b/src/services/tabs.fg.shadow.ts index b53e2ae4b..afbfa3c32 100644 --- a/src/services/tabs.fg.shadow.ts +++ b/src/services/tabs.fg.shadow.ts @@ -16,6 +16,8 @@ export async function loadInShadowMode(): Promise { Tabs.updateUrlCounter(tab.url, 1) } + Tabs.shadowReady = true + // Call deferred event handlers if (Tabs.deferredEventHandling.length) { Logs.warn('Tabs: Deferred event handlers:', Tabs.deferredEventHandling.length) @@ -31,6 +33,7 @@ export function unloadShadowed(): void { Tabs.urlsInUse = {} Tabs.list = [] Tabs.shadowMode = false + Tabs.shadowReady = false } export function setupShadowListeners(): void { @@ -57,7 +60,7 @@ export function resetShadowListeners(): void { function onShadowTabCreated(tab: browser.tabs.Tab): void { if (tab.windowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.shadowReady) { Tabs.deferredEventHandling.push(() => onShadowTabCreated(tab)) return } @@ -79,7 +82,7 @@ function onShadowTabUpdated( tab: browser.tabs.Tab ): void { if (tab.windowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.shadowReady) { Tabs.deferredEventHandling.push(() => onShadowTabUpdated(tabId, change, tab)) return } @@ -102,7 +105,7 @@ function onShadowTabUpdated( function onShadowTabRemoved(tabId: ID, info: browser.tabs.RemoveInfo): void { if (info.windowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.shadowReady) { Tabs.deferredEventHandling.push(() => onShadowTabRemoved(tabId, info)) return } @@ -130,7 +133,7 @@ function onShadowTabRemoved(tabId: ID, info: browser.tabs.RemoveInfo): void { function onShadowTabMoved(id: ID, info: browser.tabs.MoveInfo): void { if (info.windowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.shadowReady) { Tabs.deferredEventHandling.push(() => onShadowTabMoved(id, info)) return } @@ -145,7 +148,7 @@ function onShadowTabMoved(id: ID, info: browser.tabs.MoveInfo): void { function onShadowTabDetached(tabId: ID, info: browser.tabs.DetachInfo): void { if (info.oldWindowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.shadowReady) { Tabs.deferredEventHandling.push(() => onShadowTabDetached(tabId, info)) return } @@ -154,7 +157,7 @@ function onShadowTabDetached(tabId: ID, info: browser.tabs.DetachInfo): void { async function onShadowTabAttached(tabId: ID, info: browser.tabs.AttachInfo): Promise { if (info.newWindowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.shadowReady) { Tabs.deferredEventHandling.push(() => onShadowTabAttached(tabId, info)) return } @@ -166,7 +169,7 @@ async function onShadowTabAttached(tabId: ID, info: browser.tabs.AttachInfo): Pr function onShadowTabActivated(info: browser.tabs.ActiveInfo): void { if (info.windowId !== Windows.id) return - if (Tabs.list.length === 0) { + if (!Tabs.shadowReady) { Tabs.deferredEventHandling.push(() => onShadowTabActivated(info)) return } diff --git a/src/services/tabs.fg.ts b/src/services/tabs.fg.ts index 5c5e104b1..0d00605bc 100644 --- a/src/services/tabs.fg.ts +++ b/src/services/tabs.fg.ts @@ -35,6 +35,7 @@ export const Tabs = { urlsInUse: {} as Record, shadowMode: false, + shadowReady: false, tabsReinitializing: false, removedTabs: [] as RemovedTabInfo[],