@@ -1303,6 +1303,17 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
13031303 > => {
13041304 const existing = sessions . get ( wc . id ) ;
13051305 if ( existing ) return Effect . succeed ( [ existing , sessions ] as const ) ;
1306+ // A guest can be destroyed while it waits for this lock, and its native
1307+ // methods throw once it is.
1308+ if ( wc . isDestroyed ( ) ) {
1309+ return Effect . fail (
1310+ new PreviewOperationError ( {
1311+ operation : "ensureControlSession" ,
1312+ webContentsId : wc . id ,
1313+ cause : new Error ( "WebContents was destroyed" ) ,
1314+ } ) ,
1315+ ) ;
1316+ }
13061317 if ( wc . isDevToolsOpened ( ) ) {
13071318 return Effect . fail (
13081319 new PreviewAutomationDevToolsOpenError ( {
@@ -1409,28 +1420,30 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
14091420 wcDebugger . on ( "message" , onMessage ) ;
14101421 wcDebugger . attach ( "1.3" ) ;
14111422 } ) ;
1412- yield * Effect . forEach (
1413- [ "Runtime.enable" , "Accessibility.enable" , "Network.enable" , "Log.enable" ] ,
1414- ( method ) =>
1415- attemptPromise (
1416- { operation : `initializeDebugger.${ method } ` , webContentsId : wc . id } ,
1417- ( ) => wcDebugger . sendCommand ( method ) ,
1418- ) ,
1419- { concurrency : "unbounded" , discard : true } ,
1420- ) ;
14211423 // Electron gives `<webview>` guests a transparent base background, and
14221424 // Chromium only paints a dark canvas for dark color-scheme pages over an
14231425 // opaque base. Without this, dark-scheme pages with no background of
14241426 // their own (text/plain, e.g. .md files) render white text on white.
14251427 // White matches the webview's existing white backing, so light pages look
14261428 // the same; Chromium still swaps in its dark canvas for dark-scheme pages.
1429+ // Sent first because a document that paints before it arrives keeps the
1430+ // transparent base until its next load.
14271431 yield * attemptPromise (
14281432 { operation : "initializeDebugger.defaultBackground" , webContentsId : wc . id } ,
14291433 ( ) =>
14301434 wcDebugger . sendCommand ( "Emulation.setDefaultBackgroundColorOverride" , {
14311435 color : { r : 255 , g : 255 , b : 255 , a : 1 } ,
14321436 } ) ,
14331437 ) ;
1438+ yield * Effect . forEach (
1439+ [ "Runtime.enable" , "Accessibility.enable" , "Network.enable" , "Log.enable" ] ,
1440+ ( method ) =>
1441+ attemptPromise (
1442+ { operation : `initializeDebugger.${ method } ` , webContentsId : wc . id } ,
1443+ ( ) => wcDebugger . sendCommand ( method ) ,
1444+ ) ,
1445+ { concurrency : "unbounded" , discard : true } ,
1446+ ) ;
14341447 return [
14351448 control ,
14361449 replaceMap ( sessions , ( copy ) => {
@@ -2415,6 +2428,30 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
24152428 ) ;
24162429 } ) ;
24172430
2431+ // Called when a guest attaches to the window, before its first document
2432+ // paints. A tab opened straight to a URL often paints before the renderer
2433+ // gets to registerWebview, which would leave that page on the transparent
2434+ // base. registerWebview reuses the session opened here.
2435+ const prepareWebview = Effect . fn ( "PreviewManager.prepareWebview" ) ( function * (
2436+ wc : Electron . WebContents ,
2437+ ) {
2438+ const webContentsId = wc . id ;
2439+ // A guest destroyed before any tab claims it has no other cleanup path.
2440+ wc . once ( "destroyed" , ( ) => {
2441+ runFork ( detachControlSession ( webContentsId ) ) ;
2442+ } ) ;
2443+ // Runs detached from the attach event, so nothing may escape. registerWebview
2444+ // opens the session again if this one did not.
2445+ yield * ensureControlSession ( wc ) . pipe (
2446+ Effect . catchCause ( ( cause ) =>
2447+ Effect . logDebug ( "Preview webview control session was not opened on attach." , {
2448+ webContentsId,
2449+ cause,
2450+ } ) ,
2451+ ) ,
2452+ ) ;
2453+ } ) ;
2454+
24182455 const navigate = Effect . fn ( "PreviewManager.navigate" ) ( function * ( tabId : string , rawUrl : string ) {
24192456 const url = yield * attempt ( { operation : "navigate.normalizeUrl" , tabId } , ( ) =>
24202457 normalizePreviewUrl ( rawUrl ) ,
@@ -4675,6 +4712,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
46754712 openPictureInPicture,
46764713 openDevTools,
46774714 pickElement,
4715+ prepareWebview,
46784716 reapplyZoom,
46794717 refresh,
46804718 registerWebview,
@@ -5011,6 +5049,7 @@ export class PreviewManager extends Context.Service<
50115049 tabId : string ,
50125050 webContentsId : number ,
50135051 ) => Effect . Effect < void , PreviewManagerError > ;
5052+ readonly prepareWebview : ( webContents : Electron . WebContents ) => Effect . Effect < void > ;
50145053 readonly navigate : ( tabId : string , url : string ) => Effect . Effect < void , PreviewManagerError > ;
50155054 readonly goBack : ( tabId : string ) => Effect . Effect < void , PreviewManagerError > ;
50165055 readonly goForward : ( tabId : string ) => Effect . Effect < void , PreviewManagerError > ;
@@ -5137,6 +5176,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
51375176 createTab : operations . createTab ,
51385177 closeTab : operations . closeTab ,
51395178 registerWebview : operations . registerWebview ,
5179+ prepareWebview : operations . prepareWebview ,
51405180 navigate : operations . navigate ,
51415181 goBack : operations . goBack ,
51425182 goForward : operations . goForward ,
0 commit comments