@@ -37,7 +37,10 @@ import {
3737 stopBrowserRecording ,
3838} from "~/browser/browserRecording" ;
3939import { resolveBrowserRecordingStopTarget } from "~/browser/browserRecordingScope" ;
40- import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore" ;
40+ import {
41+ acquireBrowserSurfaceActivity ,
42+ useBrowserSurfaceStore ,
43+ } from "~/browser/browserSurfaceStore" ;
4144import { browserDefaultOpenViewport , resolveBrowserDefaults } from "~/browser/browserDefaults" ;
4245import { runBrowserViewportMutation } from "~/browser/browserViewportActions" ;
4346import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId" ;
@@ -98,7 +101,7 @@ const waitForDesktopOverlay = async (
98101 operation,
99102 requestId,
100103 } ) ;
101- if ( state . desktopByTabId [ tabId ] && previewBridge ) {
104+ if ( state . desktopByTabId [ tabId ] && previewBridge && isPreviewWebviewRendering ( runtimeTabId ) ) {
102105 const status = await previewBridge . automation . status ( runtimeTabId ) ;
103106 if ( status . available ) return ;
104107 }
@@ -121,6 +124,11 @@ const findPreviewWebview = (tabId: string): ExecutablePreviewWebview | null =>
121124 ( candidate ) => candidate . getAttribute ( "data-preview-tab" ) === tabId ,
122125 ) ?? null ;
123126
127+ const isPreviewWebviewRendering = ( runtimeTabId : string ) : boolean => {
128+ const wrapper = findPreviewWebview ( runtimeTabId ) ?. closest < HTMLElement > ( "[data-preview-viewport]" ) ;
129+ return wrapper ?. getAttribute ( "data-preview-rendering" ) === "active" ;
130+ } ;
131+
124132const readWebviewViewport = async (
125133 webview : ExecutablePreviewWebview ,
126134) : Promise < PreviewRenderedViewportSize | null > => {
@@ -212,8 +220,12 @@ const currentStatus = async (
212220 const visible = runtimeTabId
213221 ? ( useBrowserSurfaceStore . getState ( ) . byTabId [ runtimeTabId ] ?. visible ?? false )
214222 : false ;
223+ const renderingActive = runtimeTabId ? isPreviewWebviewRendering ( runtimeTabId ) : false ;
215224 const viewportSetting = snapshot ? ( snapshot . viewport ?? FILL_PREVIEW_VIEWPORT ) : undefined ;
216- const viewport = runtimeTabId ? await readRenderedViewport ( runtimeTabId ) . catch ( ( ) => null ) : null ;
225+ const viewport =
226+ runtimeTabId && renderingActive
227+ ? await readRenderedViewport ( runtimeTabId ) . catch ( ( ) => null )
228+ : null ;
217229 const viewportStatus = {
218230 ...( viewportSetting === undefined ? { } : { viewportSetting } ) ,
219231 ...( viewport === null ? { } : { viewport } ) ,
@@ -307,6 +319,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
307319 threadId : request . threadId ,
308320 } ;
309321 let tabId = request . tabId ?? null ;
322+ const browserActivity = { release : null as ( ( ) => void ) | null } ;
310323 try {
311324 let state = readThreadPreviewState ( threadRef ) ;
312325 const needsSessionSync = needsPreviewAutomationSessionSync ( state , request . tabId ) ;
@@ -340,6 +353,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
340353 }
341354 const readyState = readThreadPreviewState ( threadRef ) ;
342355 const runtimeTabId = previewRuntimeTabId ( threadRef , readyState . serverEpoch , readyTabId ) ;
356+ browserActivity . release ??= acquireBrowserSurfaceActivity ( runtimeTabId ) ;
343357 await waitForDesktopOverlay (
344358 threadRef ,
345359 request . requestId ,
@@ -440,14 +454,7 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
440454 usePreviewMiniPlayerStore . getState ( ) . open ( threadRef , activeTabId ) ;
441455 }
442456 if ( activeSnapshot && previewAutomationOpenNeedsOverlay ( input , activeSnapshot ) ) {
443- await waitForDesktopOverlay (
444- threadRef ,
445- request . requestId ,
446- activeTabId ,
447- activeRuntimeTabId ,
448- request . operation ,
449- request . timeoutMs ,
450- ) ;
457+ await requireReadyTab ( ) ;
451458 }
452459 if ( shouldPresentPreview ) {
453460 // React commits the thread-bound surface asynchronously. Settle
@@ -678,6 +685,8 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
678685 tabId,
679686 cause,
680687 } ) ;
688+ } finally {
689+ browserActivity . release ?.( ) ;
681690 }
682691 } ,
683692 [ environmentId , listPreviews , open , registry , resize ] ,
0 commit comments