Skip to content

Commit 08bcce8

Browse files
fix(desktop): make capture transitions atomic
Co-authored-by: maria <[email protected]>
1 parent 7cc6a23 commit 08bcce8

2 files changed

Lines changed: 117 additions & 29 deletions

File tree

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,80 @@ describe("PreviewManager", () => {
18361836
),
18371837
);
18381838

1839+
effectIt.effect("does not commit capture transitions when throttling updates fail", () =>
1840+
withManager((manager) =>
1841+
Effect.gen(function* () {
1842+
const setBackgroundThrottling = vi.fn<(enabled: boolean) => void>();
1843+
const capturePage = vi.fn(async () => ({
1844+
toJPEG: () => Buffer.from("recording-frame"),
1845+
getSize: () => ({ width: 1280, height: 720 }),
1846+
}));
1847+
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
1848+
1849+
yield* manager.createTab("tab_capture_throttling_failure");
1850+
yield* manager.registerWebview("tab_capture_throttling_failure", 42);
1851+
yield* manager.setMainWindow({
1852+
isDestroyed: () => false,
1853+
once: vi.fn(),
1854+
webContents: { setBackgroundThrottling },
1855+
} as never);
1856+
1857+
setBackgroundThrottling.mockImplementationOnce(() => {
1858+
throw new Error("start throttling update failed");
1859+
});
1860+
const failedStart = yield* Effect.exit(
1861+
manager.startRecording("tab_capture_throttling_failure"),
1862+
);
1863+
expect(Exit.isFailure(failedStart)).toBe(true);
1864+
1865+
yield* manager.startRecording("tab_capture_throttling_failure");
1866+
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [false]]);
1867+
1868+
setBackgroundThrottling.mockImplementationOnce(() => {
1869+
throw new Error("stop throttling update failed");
1870+
});
1871+
const failedStop = yield* Effect.exit(
1872+
manager.stopRecording("tab_capture_throttling_failure"),
1873+
);
1874+
expect(Exit.isFailure(failedStop)).toBe(true);
1875+
1876+
yield* manager.stopRecording("tab_capture_throttling_failure");
1877+
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [false], [true], [true]]);
1878+
}),
1879+
),
1880+
);
1881+
1882+
effectIt.effect("does not publish a replacement window when capture reconciliation fails", () =>
1883+
withManager((manager) =>
1884+
Effect.gen(function* () {
1885+
const setBackgroundThrottling = vi.fn(() => {
1886+
throw new Error("replacement throttling update failed");
1887+
});
1888+
const capturePage = vi.fn(async () => ({
1889+
toJPEG: () => Buffer.from("recording-frame"),
1890+
getSize: () => ({ width: 1280, height: 720 }),
1891+
}));
1892+
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
1893+
1894+
yield* manager.createTab("tab_capture_replacement_failure");
1895+
yield* manager.registerWebview("tab_capture_replacement_failure", 42);
1896+
yield* manager.startRecording("tab_capture_replacement_failure");
1897+
1898+
const failedReplacement = yield* Effect.exit(
1899+
manager.setMainWindow({
1900+
isDestroyed: () => false,
1901+
once: vi.fn(),
1902+
webContents: { setBackgroundThrottling },
1903+
} as never),
1904+
);
1905+
expect(Exit.isFailure(failedReplacement)).toBe(true);
1906+
1907+
yield* manager.stopRecording("tab_capture_replacement_failure");
1908+
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
1909+
}),
1910+
),
1911+
);
1912+
18391913
effectIt.effect("releases frame capture when the main window closes", () =>
18401914
withManager((manager) =>
18411915
Effect.gen(function* () {
@@ -2199,13 +2273,16 @@ describe("PreviewManager", () => {
21992273
effectIt.effect("shares background frame capture between recording and picture-in-picture", () =>
22002274
withManager((manager) =>
22012275
Effect.gen(function* () {
2276+
const setBackgroundThrottling = vi.fn();
2277+
const mainWindowWebContents = { setBackgroundThrottling };
22022278
const jpeg = Buffer.from("shared-preview-frame");
22032279
const capturePage = vi.fn(async () => ({
22042280
toJPEG: () => jpeg,
22052281
getSize: () => ({ width: 1280, height: 720 }),
22062282
}));
22072283
fromId.mockReturnValue({
22082284
id: 42,
2285+
hostWebContents: mainWindowWebContents,
22092286
isDestroyed: () => false,
22102287
getType: () => "webview",
22112288
getURL: () => "https://example.com",
@@ -2258,6 +2335,12 @@ describe("PreviewManager", () => {
22582335
const states: PreviewManager.PreviewTabState[] = [];
22592336
const recordingFrames: DesktopPreviewRecordingFrame[] = [];
22602337

2338+
yield* manager.setMainWindow({
2339+
isDestroyed: () => false,
2340+
once: vi.fn(),
2341+
webContents: mainWindowWebContents,
2342+
} as never);
2343+
22612344
yield* manager.subscribeStateChanges((_tabId, state) =>
22622345
Effect.sync(() => {
22632346
states.push(state);
@@ -2272,6 +2355,7 @@ describe("PreviewManager", () => {
22722355
yield* manager.registerWebview("tab_pip", 42);
22732356
yield* manager.openPictureInPicture("tab_pip");
22742357

2358+
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
22752359
expect(browserWindowConstructor).toHaveBeenCalledWith(
22762360
expect.objectContaining({
22772361
alwaysOnTop: true,
@@ -2317,6 +2401,7 @@ describe("PreviewManager", () => {
23172401
expect(recordingFrames).toHaveLength(1);
23182402

23192403
yield* manager.stopRecording("tab_pip");
2404+
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
23202405
const framesBeforePictureInPictureOnlyTick = pictureInPictureSend.mock.calls.length;
23212406
yield* TestClock.adjust(100);
23222407
expect(capturePage).toHaveBeenCalledTimes(3);
@@ -2326,6 +2411,7 @@ describe("PreviewManager", () => {
23262411
expect(recordingFrames).toHaveLength(1);
23272412

23282413
yield* manager.closePictureInPicture("tab_pip");
2414+
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
23292415
expect(pictureInPictureWindow.close).toHaveBeenCalledOnce();
23302416
expect(states.at(-1)?.pictureInPicture).toBe(false);
23312417
const capturesAfterClose = capturePage.mock.calls.length;

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -585,25 +585,25 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
585585
),
586586
);
587587
});
588-
const setFrameCaptureBackgroundThrottling = Effect.fnUntraced(function* (enabled: boolean) {
589-
const mainWindow = yield* Ref.get(mainWindowRef);
590-
if (Option.isNone(mainWindow) || mainWindow.value.isDestroyed()) return;
588+
const setWindowBackgroundThrottling = Effect.fnUntraced(function* (
589+
window: BrowserWindow,
590+
enabled: boolean,
591+
) {
592+
if (window.isDestroyed()) return;
591593
yield* attempt({ operation: "frameCapture.setBackgroundThrottling" }, () =>
592-
mainWindow.value.webContents.setBackgroundThrottling(enabled),
593-
).pipe(
594-
Effect.catch((error) =>
595-
Effect.logWarning("Failed to update preview frame capture throttling.", {
596-
enabled,
597-
error,
598-
}),
599-
),
594+
window.webContents.setBackgroundThrottling(enabled),
600595
);
601596
});
597+
const setFrameCaptureBackgroundThrottling = Effect.fnUntraced(function* (enabled: boolean) {
598+
const mainWindow = yield* Ref.get(mainWindowRef);
599+
if (Option.isNone(mainWindow)) return;
600+
yield* setWindowBackgroundThrottling(mainWindow.value, enabled);
601+
});
602602
const stopFrameCapture = Effect.fn("PreviewManager.stopFrameCapture")(function* (
603603
tabId: string,
604604
consumer: FrameCaptureConsumer,
605605
) {
606-
const captureScope = yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
606+
yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
607607
Effect.gen(function* () {
608608
const current = sessions.get(tabId);
609609
if (!current || !current.consumers.has(consumer)) {
@@ -627,10 +627,12 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
627627
}
628628
return [current.scope, remainingSessions] as const;
629629
}),
630+
).pipe(
631+
Effect.flatMap((captureScope) =>
632+
captureScope ? Scope.close(captureScope, Exit.void).pipe(Effect.ignore) : Effect.void,
633+
),
634+
Effect.uninterruptible,
630635
);
631-
if (captureScope) {
632-
yield* Scope.close(captureScope, Exit.void).pipe(Effect.ignore);
633-
}
634636
});
635637

636638
const stopAllRecordings = Effect.fn("PreviewManager.stopAllRecordings")(function* () {
@@ -1722,25 +1724,25 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
17221724
yield* Fiber.join(mainWindowCleanupFiber);
17231725
mainWindowCleanupFiber = undefined;
17241726
}
1725-
frameCaptureWindowOpen = true;
1726-
yield* Ref.set(mainWindowRef, Option.some(window));
17271727
yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
17281728
Effect.gen(function* () {
17291729
if (sessions.size > 0) {
1730-
yield* setFrameCaptureBackgroundThrottling(false);
1730+
yield* setWindowBackgroundThrottling(window, false);
17311731
}
1732+
yield* Ref.set(mainWindowRef, Option.some(window));
1733+
frameCaptureWindowOpen = true;
1734+
window.once("closed", () => {
1735+
frameCaptureWindowOpen = false;
1736+
mainWindowCleanupFiber = runFork(
1737+
Effect.all([closeAllPictureInPicture(), stopAllRecordings()], {
1738+
concurrency: "unbounded",
1739+
discard: true,
1740+
}).pipe(Effect.ignore),
1741+
);
1742+
});
17321743
return [undefined, sessions] as const;
17331744
}),
1734-
);
1735-
window.once("closed", () => {
1736-
frameCaptureWindowOpen = false;
1737-
mainWindowCleanupFiber = runFork(
1738-
Effect.all([closeAllPictureInPicture(), stopAllRecordings()], {
1739-
concurrency: "unbounded",
1740-
discard: true,
1741-
}).pipe(Effect.ignore),
1742-
);
1743-
});
1745+
).pipe(Effect.uninterruptible);
17441746
});
17451747

17461748
const createTabUnlocked = Effect.fn("PreviewManager.createTabUnlocked")(function* (
@@ -2680,7 +2682,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
26802682
}),
26812683
] as const;
26822684
});
2683-
});
2685+
}).pipe(Effect.uninterruptible);
26842686
if (!created) return;
26852687
yield* capturePreviewFrame(tabId).pipe(
26862688
Effect.catch((error) =>

0 commit comments

Comments
 (0)