Skip to content

Commit 92a7560

Browse files
committed
fix(desktop): skip superseded scaled capture setup
1 parent 4740c13 commit 92a7560

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Schema from "effect/Schema";
88
import * as Stream from "effect/Stream";
99
import * as TestClock from "effect/testing/TestClock";
1010
import * as NodeEvents from "node:events";
11+
import { vi } from "vite-plus/test";
1112

1213
import * as DesktopBrowserHost from "./DesktopBrowserHost.ts";
1314

@@ -222,6 +223,48 @@ describe("DesktopBrowserHost", () => {
222223
}).pipe(Effect.scoped),
223224
);
224225

226+
it.effect("never starts a stale native copy after navigation during the transform probe", () =>
227+
Effect.gen(function* () {
228+
const host = yield* DesktopBrowserHost.make;
229+
const debuggee = makeDebuggee();
230+
const probeStarted = Promise.withResolvers<void>();
231+
const probe = Promise.withResolvers<boolean>();
232+
let probing = true;
233+
const capturePage = vi.fn(async () => ({ toDataURL: () => "data:image/png;base64,eA==" }));
234+
Object.assign(debuggee.tab.webContents, {
235+
id: 42,
236+
capturePage,
237+
hostWebContents: {
238+
isDestroyed: () => false,
239+
executeJavaScript: async () => {
240+
if (probing) {
241+
probing = false;
242+
probeStarted.resolve();
243+
return probe.promise;
244+
}
245+
return true;
246+
},
247+
},
248+
});
249+
host.attach(key, debuggee.tab);
250+
const replies = yield* watchReplies(host);
251+
yield* command(host, 1, "Page.captureScreenshot");
252+
yield* Effect.promise(() => probeStarted.promise);
253+
yield* command(host, 2, "Page.captureScreenshot");
254+
const sent = debuggee.nextSend();
255+
debuggee.navigate();
256+
yield* Effect.promise(() => sent);
257+
expect(debuggee.sent).toEqual([2]);
258+
expect(capturePage).toHaveBeenCalledOnce();
259+
probe.resolve(true);
260+
yield* settle;
261+
expect(capturePage).toHaveBeenCalledOnce();
262+
expect(yield* Queue.take(replies)).toBe(1);
263+
debuggee.release();
264+
expect(yield* Queue.take(replies)).toBe(2);
265+
}).pipe(Effect.scoped),
266+
);
267+
225268
it.effect("drops screenshots a released relay left waiting", () =>
226269
Effect.gen(function* () {
227270
const host = yield* DesktopBrowserHost.make;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ export const make = Effect.gen(function* () {
346346
webContents,
347347
Effect.tryPromise(sendCurrent),
348348
"data-preview-server-tab",
349+
undefined,
350+
current,
349351
),
350352
).catch((error: unknown) => {
351353
throw Cause.isUnknownError(error) ? error.cause : error;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ export const resetPreviewCapture = (wc: Electron.WebContents) => captureLocks.de
1111

1212
// Chromium rasterizes CSS-scaled guests at their displayed size, including CDP screenshots.
1313
// Freeze only that preview while capturing unscaled pixels; never change its viewport.
14-
export const withUnscaledPreviewCapture = <A, E, R>(
14+
export const withUnscaledPreviewCapture = Effect.fn("PreviewCapture.withUnscaledPreviewCapture")(<
15+
A,
16+
E,
17+
R,
18+
>(
1519
tabId: string,
1620
wc: Electron.WebContents,
1721
capture: Effect.Effect<A, E, R>,
1822
tabAttribute: "data-preview-tab" | "data-preview-server-tab" = "data-preview-tab",
1923
unscaledCaptureTimeoutMs?: number,
24+
current: () => boolean = () => true,
2025
) => {
26+
const requireCurrent = Effect.try(() => {
27+
if (!current()) throw new Error("The page this screenshot was for has moved on.");
28+
});
2129
let lock = captureLocks.get(wc);
2230
if (!lock) {
2331
lock = Semaphore.makeUnsafe(1);
@@ -47,7 +55,9 @@ export const withUnscaledPreviewCapture = <A, E, R>(
4755
Effect.orElseSucceed(() => false),
4856
);
4957
if (scaled !== true) return yield* unscaledCapture;
58+
yield* requireCurrent;
5059
const frozen = yield* nativeCapture(Effect.tryPromise((_signal) => wc.capturePage()));
60+
yield* requireCurrent;
5161
const token = `preview-capture-${++captureSequence}`;
5262
// The renderer checks delayed IPC expiry outside the Effect runtime.
5363
const deadline = (yield* Clock.Clock.defaultValue().currentTimeMillis) + 5_000;
@@ -123,7 +133,7 @@ export const withUnscaledPreviewCapture = <A, E, R>(
123133
// Bound setup and queueing; native relay copies retain their queue slot until settled.
124134
Effect.timeout("5 seconds"),
125135
);
126-
};
136+
});
127137

128138
export const capturePreviewPage = (tabId: string, wc: Electron.WebContents) =>
129139
withUnscaledPreviewCapture(

0 commit comments

Comments
 (0)