Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,83 @@ describe("PreviewManager", () => {
),
);

effectIt.effect("keeps window unthrottled until the final frame capture stops", () =>
withManager((manager) =>
Effect.gen(function* () {
const setBackgroundThrottling = vi.fn();
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
const webContentsById = new Map([
[41, makeTestPreviewWebContents(capturePage, 41)],
[42, makeTestPreviewWebContents(capturePage, 42)],
]);
fromId.mockImplementation((id) =>
id === undefined ? null : (webContentsById.get(id) ?? null),
);

yield* manager.createTab("tab_capture_throttling_1");
yield* manager.createTab("tab_capture_throttling_2");
yield* manager.registerWebview("tab_capture_throttling_1", 41);
yield* manager.registerWebview("tab_capture_throttling_2", 42);
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling },
} as never);

yield* manager.startRecording("tab_capture_throttling_1");
yield* manager.startRecording("tab_capture_throttling_2");
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);

yield* manager.stopRecording("tab_capture_throttling_1");
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);

yield* manager.stopRecording("tab_capture_throttling_2");
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
}),
),
);

effectIt.effect("releases frame capture when the main window closes", () =>
withManager((manager) =>
Effect.gen(function* () {
let closeMainWindow: (() => void) | undefined;
const firstWindowThrottling = vi.fn();
const replacementWindowThrottling = vi.fn();
const capturePage = vi.fn(async () => ({
toJPEG: () => Buffer.from("recording-frame"),
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));

yield* manager.createTab("tab_window_close_recording");
yield* manager.registerWebview("tab_window_close_recording", 42);
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn((event: string, listener: () => void) => {
if (event === "closed") closeMainWindow = listener;
}),
webContents: { setBackgroundThrottling: firstWindowThrottling },
} as never);
yield* manager.startRecording("tab_window_close_recording");
expect(firstWindowThrottling.mock.calls).toEqual([[false]]);

closeMainWindow?.();
yield* Effect.yieldNow;
yield* Effect.yieldNow;

yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling: replacementWindowThrottling },
} as never);
expect(replacementWindowThrottling).not.toHaveBeenCalled();
}),
),
);

effectIt.effect("captures hidden preview recordings independently for concurrent tabs", () =>
withManager((manager) =>
Effect.gen(function* () {
Expand Down
85 changes: 63 additions & 22 deletions apps/desktop/src/preview/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,37 +583,62 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
),
);
});
const setFrameCaptureBackgroundThrottling = Effect.fnUntraced(function* (enabled: boolean) {
const mainWindow = yield* Ref.get(mainWindowRef);
if (Option.isNone(mainWindow) || mainWindow.value.isDestroyed()) return;
yield* attempt({ operation: "frameCapture.setBackgroundThrottling" }, () =>
mainWindow.value.webContents.setBackgroundThrottling(enabled),
).pipe(
Effect.catch((error) =>
Effect.logWarning("Failed to update preview frame capture throttling.", {
enabled,
error,
}),
),
);
});
const stopFrameCapture = Effect.fn("PreviewManager.stopFrameCapture")(function* (
tabId: string,
consumer: FrameCaptureConsumer,
) {
const captureScope = yield* SynchronizedRef.modify(frameCaptureSessionsRef, (sessions) => {
const current = sessions.get(tabId);
if (!current || !current.consumers.has(consumer)) {
return [undefined, sessions] as const;
}
const consumers = new Set(current.consumers);
consumers.delete(consumer);
Comment thread
t3-code[bot] marked this conversation as resolved.
if (consumers.size > 0) {
return [
undefined,
replaceMap(sessions, (copy) => {
copy.set(tabId, { ...current, consumers });
}),
] as const;
}
return [
current.scope,
replaceMap(sessions, (copy) => {
const captureScope = yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
Effect.gen(function* () {
const current = sessions.get(tabId);
if (!current || !current.consumers.has(consumer)) {
return [undefined, sessions] as const;
}
const consumers = new Set(current.consumers);
consumers.delete(consumer);
if (consumers.size > 0) {
return [
undefined,
replaceMap(sessions, (copy) => {
copy.set(tabId, { ...current, consumers });
}),
] as const;
}
const remainingSessions = replaceMap(sessions, (copy) => {
copy.delete(tabId);
}),
] as const;
});
});
if (remainingSessions.size === 0) {
yield* setFrameCaptureBackgroundThrottling(true);
}
return [current.scope, remainingSessions] as const;
}),
);
if (captureScope) {
yield* Scope.close(captureScope, Exit.void).pipe(Effect.ignore);
}
});

const stopAllRecordings = Effect.fn("PreviewManager.stopAllRecordings")(function* () {
const sessions = yield* SynchronizedRef.get(frameCaptureSessionsRef);
yield* Effect.forEach(sessions.keys(), (tabId) => stopFrameCapture(tabId, "recording"), {
concurrency: "unbounded",
discard: true,
});
});
Comment thread
t3-code[bot] marked this conversation as resolved.

const deliverEvent = (
eventKind: "state-change" | "recording-frame" | "pointer-event",
tabId: string,
Expand Down Expand Up @@ -1692,8 +1717,21 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
window: BrowserWindow,
) {
yield* Ref.set(mainWindowRef, Option.some(window));
yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
Effect.gen(function* () {
if (sessions.size > 0) {
yield* setFrameCaptureBackgroundThrottling(false);
}
return [undefined, sessions] as const;
}),
);
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
window.once("closed", () => {
runFork(closeAllPictureInPicture());
runFork(
Effect.all([closeAllPictureInPicture(), stopAllRecordings()], {
concurrency: "unbounded",
discard: true,
}),
);
});
});

Expand Down Expand Up @@ -2616,6 +2654,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
}),
] as const;
}
if (sessions.size === 0) {
yield* setFrameCaptureBackgroundThrottling(false);
}
const scope = yield* Scope.fork(parentScope, "sequential");
yield* Effect.forkIn(Effect.forever(captureNextFrame), scope);
return [
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/window/DesktopWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ describe("DesktopWindow", () => {
assert.isUndefined(createdWindowOptions[0]?.x);
assert.isUndefined(createdWindowOptions[0]?.y);
assert.isTrue(createdWindowOptions[0]?.disableAutoHideCursor);
assert.isFalse(createdWindowOptions[0]?.webPreferences?.backgroundThrottling);
assert.isUndefined(createdWindowOptions[0]?.webPreferences?.backgroundThrottling);
assert.deepEqual(fakeWindow.setAutoHideCursor.mock.calls, [[false]]);
assert.deepEqual(fakeWindow.loadURL.mock.calls[0], ["t3code-dev://app/"]);
assert.equal(fakeWindow.openDevTools.mock.calls.length, 1);
Expand Down
1 change: 0 additions & 1 deletion apps/desktop/src/window/DesktopWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ export const make = Effect.gen(function* () {
...getWindowTitleBarOptions(shouldUseDarkColors, environment.platform),
webPreferences: {
preload: environment.preloadPath,
backgroundThrottling: false,
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
Expand Down
Loading