Skip to content

Commit 51e067e

Browse files
committed
perf(desktop): skip duplicate browser updates
1 parent 55c9093 commit 51e067e

4 files changed

Lines changed: 127 additions & 7 deletions

File tree

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,9 +2448,7 @@ describe("PreviewManager", () => {
24482448
const framesBeforePictureInPictureOnlyTick = pictureInPictureSend.mock.calls.length;
24492449
yield* TestClock.adjust(100);
24502450
expect(capturePage).toHaveBeenCalledTimes(3);
2451-
expect(pictureInPictureSend.mock.calls.length).toBeGreaterThan(
2452-
framesBeforePictureInPictureOnlyTick,
2453-
);
2451+
expect(pictureInPictureSend.mock.calls.length).toBe(framesBeforePictureInPictureOnlyTick);
24542452
expect(recordingFrames).toHaveLength(1);
24552453

24562454
setBackgroundThrottling.mockImplementationOnce(() => {
@@ -2467,6 +2465,51 @@ describe("PreviewManager", () => {
24672465
),
24682466
);
24692467

2468+
effectIt.effect("delivers recording frames only when pixels change", () =>
2469+
withManager((manager) =>
2470+
Effect.gen(function* () {
2471+
const unchanged = Buffer.from("unchanged-preview-frame");
2472+
const changed = Buffer.from("changed-preview-frame");
2473+
let captureIndex = 0;
2474+
const capturePage = vi.fn(async () => {
2475+
const jpeg = captureIndex < 2 ? unchanged : changed;
2476+
captureIndex += 1;
2477+
return {
2478+
toJPEG: () => jpeg,
2479+
getSize: () => ({ width: 1280, height: 720 }),
2480+
};
2481+
});
2482+
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
2483+
const frames: DesktopPreviewRecordingFrame[] = [];
2484+
2485+
yield* manager.subscribeRecordingFrames((frame) =>
2486+
Effect.sync(() => {
2487+
frames.push(frame);
2488+
}),
2489+
);
2490+
yield* manager.createTab("tab_unchanged_frame");
2491+
yield* manager.registerWebview("tab_unchanged_frame", 42);
2492+
yield* manager.startRecording("tab_unchanged_frame");
2493+
2494+
expect(frames.map((frame) => frame.data)).toEqual([unchanged.toString("base64")]);
2495+
2496+
yield* TestClock.adjust(100);
2497+
2498+
expect(capturePage).toHaveBeenCalledTimes(2);
2499+
expect(frames.map((frame) => frame.data)).toEqual([unchanged.toString("base64")]);
2500+
2501+
yield* TestClock.adjust(100);
2502+
2503+
expect(capturePage).toHaveBeenCalledTimes(3);
2504+
expect(frames.map((frame) => frame.data)).toEqual([
2505+
unchanged.toString("base64"),
2506+
changed.toString("base64"),
2507+
]);
2508+
yield* manager.stopRecording("tab_unchanged_frame");
2509+
}),
2510+
),
2511+
);
2512+
24702513
effectIt.effect("retries a cold hidden-tab capture without dropping recording", () =>
24712514
withManager((manager) =>
24722515
Effect.gen(function* () {

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ type FrameCaptureConsumer = "picture-in-picture" | "recording";
381381
interface FrameCaptureSession {
382382
readonly scope: Scope.Closeable;
383383
readonly consumers: ReadonlySet<FrameCaptureConsumer>;
384+
readonly lastFrame: Buffer | null;
384385
}
385386

386387
interface PictureInPictureSession {
@@ -2549,18 +2550,38 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
25492550
tabId,
25502551
webContentsId: wc.id,
25512552
},
2552-
() => image.toJPEG(RECORDING_JPEG_QUALITY).toString("base64"),
2553+
() => image.toJPEG(RECORDING_JPEG_QUALITY),
25532554
);
2555+
const deliveryCaptureSession = yield* SynchronizedRef.modify(
2556+
frameCaptureSessionsRef,
2557+
(sessions) => {
2558+
const current = sessions.get(tabId);
2559+
if (
2560+
current?.scope !== captureSession.scope ||
2561+
current.lastFrame?.equals(encoded) === true
2562+
) {
2563+
return [undefined, sessions] as const;
2564+
}
2565+
const next = { ...current, lastFrame: encoded };
2566+
return [
2567+
next,
2568+
replaceMap(sessions, (copy) => {
2569+
copy.set(tabId, next);
2570+
}),
2571+
] as const;
2572+
},
2573+
);
2574+
if (!deliveryCaptureSession) return;
25542575
const receivedAt = yield* currentIso;
25552576
const frame: DesktopPreviewRecordingFrame = {
25562577
tabId,
2557-
data: encoded,
2578+
data: encoded.toString("base64"),
25582579
width: size.width,
25592580
height: size.height,
25602581
receivedAt,
25612582
};
25622583
const deliveries: Array<Effect.Effect<void>> = [];
2563-
if (currentCaptureSession.consumers.has("recording")) {
2584+
if (deliveryCaptureSession.consumers.has("recording")) {
25642585
const listeners = yield* Ref.get(recordingFrameListenersRef);
25652586
deliveries.push(
25662587
Effect.forEach(
@@ -2570,7 +2591,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
25702591
),
25712592
);
25722593
}
2573-
if (currentCaptureSession.consumers.has("picture-in-picture")) {
2594+
if (deliveryCaptureSession.consumers.has("picture-in-picture")) {
25742595
const pictureInPictureWindow = (yield* SynchronizedRef.get(pictureInPictureSessionsRef)).get(
25752596
tabId,
25762597
)?.window;
@@ -2672,6 +2693,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
26722693
copy.set(tabId, {
26732694
...current,
26742695
consumers: new Set([...current.consumers, consumer]),
2696+
lastFrame: null,
26752697
});
26762698
}),
26772699
] as const;
@@ -2687,6 +2709,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
26872709
copy.set(tabId, {
26882710
scope,
26892711
consumers: new Set([consumer]),
2712+
lastFrame: null,
26902713
});
26912714
}),
26922715
] as const;

‎apps/web/src/previewStateStore.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
rememberPreviewUrl,
2121
removePreviewThread,
2222
resetPreviewStateForTests,
23+
subscribeThreadPreviewState,
2324
setActivePreviewTab,
2425
updatePreviewServerSnapshot,
2526
} from "./previewStateStore";
@@ -331,6 +332,38 @@ describe("previewStateStore (single-tab)", () => {
331332
expect(state.snapshot?.canGoBack).toBe(false);
332333
});
333334

335+
it("does not publish duplicate desktop browser state", () => {
336+
const snapshot = makeSnapshot();
337+
applyPreviewServerSnapshot(ref, snapshot);
338+
const overlay = {
339+
hasWebContents: true,
340+
canGoBack: true,
341+
canGoForward: false,
342+
loading: false,
343+
zoomFactor: 1,
344+
pictureInPicture: false,
345+
colorScheme: "system" as const,
346+
audioMuted: false,
347+
audible: false,
348+
controller: "none" as const,
349+
favicon: {
350+
dataUrl: "data:image/png;base64,AA==",
351+
pageUrl: "https://example.com",
352+
capturedAt: 1,
353+
},
354+
};
355+
const updates: unknown[] = [];
356+
const unsubscribe = subscribeThreadPreviewState(ref, (state) => {
357+
updates.push(state);
358+
});
359+
360+
applyPreviewDesktopState(ref, snapshot.tabId, overlay);
361+
applyPreviewDesktopState(ref, snapshot.tabId, { ...overlay, favicon: { ...overlay.favicon } });
362+
unsubscribe();
363+
364+
expect(updates).toHaveLength(1);
365+
});
366+
334367
it("retains multiple tabs and switches active desktop state", () => {
335368
const first = makeSnapshot();
336369
const second = { ...makeSnapshot(), tabId: "tab_2", updatedAt: "2026-01-02T00:00:00.000Z" };

‎apps/web/src/previewStateStore.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,27 @@ export function applyPreviewDesktopState(
374374
overlay: DesktopPreviewOverlay | null,
375375
): void {
376376
updateThreadPreviewState(ref, (current) => {
377+
const previous = current.desktopByTabId[tabId] ?? null;
378+
if (
379+
previous === overlay ||
380+
(previous !== null &&
381+
overlay !== null &&
382+
previous.hasWebContents === overlay.hasWebContents &&
383+
previous.canGoBack === overlay.canGoBack &&
384+
previous.canGoForward === overlay.canGoForward &&
385+
previous.loading === overlay.loading &&
386+
previous.zoomFactor === overlay.zoomFactor &&
387+
previous.pictureInPicture === overlay.pictureInPicture &&
388+
previous.colorScheme === overlay.colorScheme &&
389+
previous.audioMuted === overlay.audioMuted &&
390+
previous.audible === overlay.audible &&
391+
previous.controller === overlay.controller &&
392+
previous.favicon?.dataUrl === overlay.favicon?.dataUrl &&
393+
previous.favicon?.pageUrl === overlay.favicon?.pageUrl &&
394+
previous.favicon?.capturedAt === overlay.favicon?.capturedAt)
395+
) {
396+
return current;
397+
}
377398
const desktopByTabId = { ...current.desktopByTabId };
378399
if (overlay) desktopByTabId[tabId] = overlay;
379400
else delete desktopByTabId[tabId];

0 commit comments

Comments
 (0)