diff --git a/src/zen/sessionstore/ZenSessionManager.sys.mjs b/src/zen/sessionstore/ZenSessionManager.sys.mjs index 0a1ae2511..0541894d3 100644 --- a/src/zen/sessionstore/ZenSessionManager.sys.mjs +++ b/src/zen/sessionstore/ZenSessionManager.sys.mjs @@ -255,7 +255,7 @@ export class nsZenSessionManager { const windows = (state.windows || []).filter( (win) => !win.isPrivate && !win.isPopup && !win.isTaskbarTab && !win.isZenUnsynced ); - let windowToClone = windows[0] || {}; + let windowToClone = windows.length > 1 ? windows[windows.length - 2] : {}; let newWindow = Cu.cloneInto(windowToClone, {}); if (windows.length < 2) { // We only want to restore the sidebar object if we found @@ -286,7 +286,7 @@ export class nsZenSessionManager { firstWindow: true, }); - resolvePromise(); + lazy.setTimeout(resolvePromise); }); } } diff --git a/src/zen/sessionstore/ZenWindowSync.sys.mjs b/src/zen/sessionstore/ZenWindowSync.sys.mjs index 366067170..792478b5c 100644 --- a/src/zen/sessionstore/ZenWindowSync.sys.mjs +++ b/src/zen/sessionstore/ZenWindowSync.sys.mjs @@ -17,7 +17,7 @@ XPCOMUtils.defineLazyPreferenceGetter(lazy, 'gWindowSyncEnabled', 'zen.window-sy XPCOMUtils.defineLazyPreferenceGetter(lazy, 'gShouldLog', 'zen.window-sync.log', true); const OBSERVING = ['browser-window-before-show']; -const INSTANT_EVENTS = ['unload']; +const INSTANT_EVENTS = ['SSWindowClosing']; const EVENTS = [ 'TabOpen', 'TabClose', @@ -203,7 +203,7 @@ class nsZenWindowSync { */ #runOnAllWindows(aWindow, aCallback) { for (let window of this.#browserWindows) { - if (window !== aWindow) { + if (window !== aWindow && !window._zenClosingWindow) { let value = aCallback(window); if (value) { return value; @@ -224,7 +224,11 @@ class nsZenWindowSync { handleEvent(aEvent) { const window = aEvent.currentTarget.ownerGlobal; - if (!window.gZenStartup.isReady || window.gZenWorkspaces?.privateWindowOrDisabled) { + if ( + !window.gZenStartup.isReady || + window.gZenWorkspaces?.privateWindowOrDisabled || + window._zenClosingWindow + ) { return; } if (INSTANT_EVENTS.includes(aEvent.type)) { @@ -658,6 +662,11 @@ class nsZenWindowSync { */ #onTabSwitchOrWindowFocus(aWindow, aPreviousTab = null) { const selectedTab = aWindow.gBrowser.selectedTab; + // On some occasions, such as when closing a window, this + // function might be called multiple times for the same tab. + if (selectedTab === this.#lastSelectedTab) { + return; + } if (aPreviousTab?._zenContentsVisible) { const otherTabToShow = this.#getActiveTabFromOtherWindows( aWindow, @@ -853,8 +862,9 @@ class nsZenWindowSync { this.#onTabSwitchOrWindowFocus(aEvent.target.ownerGlobal, previousTab); } - on_unload(aEvent) { + on_SSWindowClosing(aEvent) { const window = aEvent.target.ownerGlobal; + window._zenClosingWindow = true; for (let eventName of EVENTS) { window.removeEventListener(eventName, this); }