Skip to content

Commit 7281787

Browse files
fix(desktop): ignore stale window close events
Co-authored-by: maria <[email protected]>
1 parent 8d5b571 commit 7281787

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,43 @@ describe("PreviewManager", () => {
19161916
),
19171917
);
19181918

1919+
effectIt.effect("ignores close events from replaced main windows", () =>
1920+
withManager((manager) =>
1921+
Effect.gen(function* () {
1922+
let closeFirstWindow: (() => void) | undefined;
1923+
const firstWindowThrottling = vi.fn();
1924+
const replacementWindowThrottling = vi.fn();
1925+
const capturePage = vi.fn(async () => ({
1926+
toJPEG: () => Buffer.from("recording-frame"),
1927+
getSize: () => ({ width: 1280, height: 720 }),
1928+
}));
1929+
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
1930+
1931+
yield* manager.createTab("tab_replaced_window_close");
1932+
yield* manager.registerWebview("tab_replaced_window_close", 42);
1933+
yield* manager.setMainWindow({
1934+
isDestroyed: () => false,
1935+
once: vi.fn((event: string, listener: () => void) => {
1936+
if (event === "closed") closeFirstWindow = listener;
1937+
}),
1938+
webContents: { setBackgroundThrottling: firstWindowThrottling },
1939+
} as never);
1940+
yield* manager.setMainWindow({
1941+
isDestroyed: () => false,
1942+
once: vi.fn(),
1943+
webContents: { setBackgroundThrottling: replacementWindowThrottling },
1944+
} as never);
1945+
1946+
closeFirstWindow?.();
1947+
yield* manager.startRecording("tab_replaced_window_close");
1948+
expect(firstWindowThrottling).not.toHaveBeenCalled();
1949+
expect(replacementWindowThrottling.mock.calls).toEqual([[false]]);
1950+
yield* manager.stopRecording("tab_replaced_window_close");
1951+
expect(replacementWindowThrottling.mock.calls).toEqual([[false], [true]]);
1952+
}),
1953+
),
1954+
);
1955+
19191956
effectIt.effect("releases frame capture when the main window closes", () =>
19201957
withManager((manager) =>
19211958
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
527527
const pictureInPictureMutationSemaphore = yield* Semaphore.make(1);
528528
const closingTabIdsRef = yield* Ref.make<ReadonlySet<string>>(new Set());
529529
let frameCaptureWindowOpen = true;
530+
let currentMainWindow: BrowserWindow | undefined;
530531
let mainWindowCleanupFiber: Fiber.Fiber<void, never> | undefined;
531532
const tabLifecycleLocks = new Map<
532533
string,
@@ -1735,8 +1736,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17351736
yield* setWindowBackgroundThrottling(window, false);
17361737
}
17371738
yield* Ref.set(mainWindowRef, Option.some(window));
1739+
currentMainWindow = window;
17381740
frameCaptureWindowOpen = true;
17391741
window.once("closed", () => {
1742+
if (currentMainWindow !== window) return;
1743+
currentMainWindow = undefined;
17401744
frameCaptureWindowOpen = false;
17411745
mainWindowCleanupFiber = runFork(
17421746
Effect.all([closeAllPictureInPicture(), stopAllRecordings()], {

0 commit comments

Comments
 (0)