Skip to content

Commit

Permalink
fix: tabs: incorrect state checking in event handlers
Browse files Browse the repository at this point in the history
(resolves #1230)
  • Loading branch information
mbnuqw committed Apr 6, 2024
1 parent c0eda1f commit e64b65b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
15 changes: 8 additions & 7 deletions src/services/tabs.fg.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ export async function load(): Promise<void> {
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 = []

Expand Down Expand Up @@ -338,13 +346,6 @@ async function restoreTabsState(): Promise<void> {
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`)
}

Expand Down
14 changes: 7 additions & 7 deletions src/services/tabs.fg.handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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<void> {
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
}
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 10 additions & 7 deletions src/services/tabs.fg.shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export async function loadInShadowMode(): Promise<void> {
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)
Expand All @@ -31,6 +33,7 @@ export function unloadShadowed(): void {
Tabs.urlsInUse = {}
Tabs.list = []
Tabs.shadowMode = false
Tabs.shadowReady = false
}

export function setupShadowListeners(): void {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -154,7 +157,7 @@ function onShadowTabDetached(tabId: ID, info: browser.tabs.DetachInfo): void {

async function onShadowTabAttached(tabId: ID, info: browser.tabs.AttachInfo): Promise<void> {
if (info.newWindowId !== Windows.id) return
if (Tabs.list.length === 0) {
if (!Tabs.shadowReady) {
Tabs.deferredEventHandling.push(() => onShadowTabAttached(tabId, info))
return
}
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/services/tabs.fg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const Tabs = {

urlsInUse: {} as Record<string, number>,
shadowMode: false,
shadowReady: false,

tabsReinitializing: false,
removedTabs: [] as RemovedTabInfo[],
Expand Down

0 comments on commit e64b65b

Please sign in to comment.