fix(desktop): stop hidden previews draining battery (#8567)

This commit is contained in:
Bilal Bakr authored and GitHub committed 2026-08-28 14:26:33 -07:00
1 parent a40aef4ccb
commit ff1761012a
9 files changed
+113 -31

No files matched your search

@@ -37,7 +37,10 @@ import {
stopBrowserRecording,
} from "~/browser/browserRecording";
import { resolveBrowserRecordingStopTarget } from "~/browser/browserRecordingScope";
import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
import {
acquireBrowserSurfaceActivity,
useBrowserSurfaceStore,
} from "~/browser/browserSurfaceStore";
import { browserDefaultOpenViewport, resolveBrowserDefaults } from "~/browser/browserDefaults";
import { runBrowserViewportMutation } from "~/browser/browserViewportActions";
import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId";
@@ -98,7 +101,7 @@ const waitForDesktopOverlay = async (
operation,
requestId,
});
if (state.desktopByTabId[tabId] && previewBridge) {
if (state.desktopByTabId[tabId] && previewBridge && isPreviewWebviewRendering(runtimeTabId)) {
const status = await previewBridge.automation.status(runtimeTabId);
if (status.available) return;
}
@@ -121,6 +124,11 @@ const findPreviewWebview = (tabId: string): ExecutablePreviewWebview | null =>
(candidate) => candidate.getAttribute("data-preview-tab") === tabId,
) ?? null;
const isPreviewWebviewRendering = (runtimeTabId: string): boolean => {
const wrapper = findPreviewWebview(runtimeTabId)?.closest<HTMLElement>("[data-preview-viewport]");
return wrapper?.getAttribute("data-preview-rendering") === "active";
};
const readWebviewViewport = async (
webview: ExecutablePreviewWebview,
): Promise<PreviewRenderedViewportSize | null> => {
@@ -212,8 +220,12 @@ const currentStatus = async (
const visible = runtimeTabId
? (useBrowserSurfaceStore.getState().byTabId[runtimeTabId]?.visible ?? false)
: false;
const renderingActive = runtimeTabId ? isPreviewWebviewRendering(runtimeTabId) : false;
const viewportSetting = snapshot ? (snapshot.viewport ?? FILL_PREVIEW_VIEWPORT) : undefined;
const viewport = runtimeTabId ? await readRenderedViewport(runtimeTabId).catch(() => null) : null;
const viewport =
runtimeTabId && renderingActive
? await readRenderedViewport(runtimeTabId).catch(() => null)
: null;
const viewportStatus = {
...(viewportSetting === undefined ? {} : { viewportSetting }),
...(viewport === null ? {} : { viewport }),
@@ -307,6 +319,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
threadId: request.threadId,
};
let tabId = request.tabId ?? null;
const browserActivity = { release: null as (() => void) | null };
try {
let state = readThreadPreviewState(threadRef);
const needsSessionSync = needsPreviewAutomationSessionSync(state, request.tabId);
@@ -340,6 +353,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
}
const readyState = readThreadPreviewState(threadRef);
const runtimeTabId = previewRuntimeTabId(threadRef, readyState.serverEpoch, readyTabId);
browserActivity.release ??= acquireBrowserSurfaceActivity(runtimeTabId);
await waitForDesktopOverlay(
threadRef,
request.requestId,
@@ -440,14 +454,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
usePreviewMiniPlayerStore.getState().open(threadRef, activeTabId);
}
if (activeSnapshot && previewAutomationOpenNeedsOverlay(input, activeSnapshot)) {
await waitForDesktopOverlay(
threadRef,
request.requestId,
activeTabId,
activeRuntimeTabId,
request.operation,
request.timeoutMs,
);
await requireReadyTab();
}
if (shouldPresentPreview) {
// React commits the thread-bound surface asynchronously. Settle
@@ -678,6 +685,8 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
tabId,
cause,
});
} finally {
browserActivity.release?.();
}
},
[environmentId, listPreviews, open, registry, resize],