@@ -235,11 +235,20 @@ function makeContext(onClose?: (context: BrowserContext) => void, initialUrl = "
235235 // Set to hold the page open after `beforeunload` until resolved, as Playwright's
236236 // `runBeforeUnload` close resolves before the page actually closes.
237237 unloadGate : null as PromiseWithResolvers < void > | null ,
238+ removed : false ,
239+ pagehideBinding : null as ( ( ) => void ) | null ,
240+ pagehideWatched : Promise . withResolvers < void > ( ) ,
241+ exposeFunction : vi . fn ( async ( _name : string , callback : ( ) => void ) => {
242+ page . pagehideBinding = callback ;
243+ page . pagehideWatched . resolve ( ) ;
244+ } ) ,
238245 close : vi . fn ( async ( options ?: { runBeforeUnload ?: boolean } ) => {
239246 if ( closed ) return ;
240247 const finish = ( ) => {
241- if ( closed ) return ;
242- page . unloadEvents . push ( "pagehide" , "visibilitychange" ) ;
248+ if ( closed || page . removed ) return ;
249+ page . unloadEvents . push ( "pagehide" ) ;
250+ page . pagehideBinding ?.( ) ;
251+ page . unloadEvents . push ( "visibilitychange" ) ;
243252 closed = true ;
244253 events . emit ( "close" ) ;
245254 } ;
@@ -1150,10 +1159,11 @@ it.live("closing a tab force-closes a page that never finishes unloading", () =>
11501159 ) . pipe ( Effect . provide ( layer ) ) ,
11511160) ;
11521161
1153- it . live ( "closing a desktop tab unloads its page before the desktop removes it" , ( ) =>
1162+ it . live ( "closing a desktop tab waits for its page to unload before the desktop removes it" , ( ) =>
11541163 Effect . scoped (
11551164 Effect . gen ( function * ( ) {
11561165 const { broker } = yield * ready ;
1166+ const manager = yield * Manager . PreviewManager ;
11571167 desktopRendersNext = true ;
11581168 const opened = yield * broker . invoke < PreviewAutomationStatus > ( {
11591169 scope,
@@ -1163,15 +1173,28 @@ it.live("closing a desktop tab unloads its page before the desktop removes it",
11631173 const tabId = PreviewTabId . make ( opened . tabId ! ) ;
11641174 const { context, browser } = desktopConnections [ 0 ] ! ;
11651175 const page = context . page ;
1176+ // The desktop removes the webview once the session closes, dropping an unload in progress.
1177+ const events = yield * manager . subscribeEvents ;
1178+ const sessionClosed = Promise . withResolvers < void > ( ) ;
1179+ yield * Effect . gen ( function * ( ) {
1180+ while ( ( yield * PubSub . take ( events ) ) . type !== "closed" ) ;
1181+ page . removed = true ;
1182+ sessionClosed . resolve ( ) ;
1183+ } ) . pipe ( Effect . forkScoped ) ;
11661184 page . beforeUnload = "listen" ;
11671185 page . unloadGate = Promise . withResolvers < void > ( ) ;
1168- yield * broker . invoke ( { scope, tabId, operation : "close" , input : { } } ) ;
1169- expect ( page . unloadEvents ) . toEqual ( [ "beforeunload" ] ) ;
1186+ const closing = yield * broker
1187+ . invoke ( { scope, tabId, operation : "close" , input : { } } )
1188+ . pipe ( Effect . forkScoped ) ;
1189+ // The page finishes unloading once the server listens for it, or too late if it never does.
1190+ yield * Effect . promise ( ( ) =>
1191+ Promise . race ( [ page . pagehideWatched . promise , sessionClosed . promise ] ) ,
1192+ ) ;
11701193 expect ( browser . close ) . not . toHaveBeenCalled ( ) ;
11711194 page . unloadGate . resolve ( ) ;
1172- yield * Effect . promise ( ( ) => desktopReleased . promise ) ;
1173- expect ( browser . close ) . toHaveBeenCalledTimes ( 1 ) ;
1195+ yield * Fiber . join ( closing ) ;
11741196 expect ( page . unloadEvents ) . toEqual ( [ "beforeunload" , "pagehide" , "visibilitychange" ] ) ;
1197+ expect ( browser . close ) . toHaveBeenCalledTimes ( 1 ) ;
11751198 } ) ,
11761199 ) . pipe ( Effect . provide ( layer ) ) ,
11771200) ;
0 commit comments