@@ -55,11 +55,15 @@ class IPCHandler<TRouter extends AnyRouter> {
5555 this . #attachSubscriptionCleanupHandlers( win ) ;
5656 }
5757
58- detachWindow ( win : BrowserWindow ) {
58+ detachWindow ( win : BrowserWindow , webContentsId ?: number ) {
5959 debug ( 'Detaching window' , win . id ) ;
6060
61+ if ( win . isDestroyed ( ) && webContentsId === undefined ) {
62+ throw new Error ( 'webContentsId is required when calling detachWindow on a destroyed window' ) ;
63+ }
64+
6165 this . #windows = this . #windows. filter ( ( w ) => w !== win ) ;
62- this . #cleanUpSubscriptions( { webContentsId : win . webContents . id } ) ;
66+ this . #cleanUpSubscriptions( { webContentsId : webContentsId ?? win . webContents . id } ) ;
6367 }
6468
6569 #cleanUpSubscriptions( {
@@ -79,23 +83,24 @@ class IPCHandler<TRouter extends AnyRouter> {
7983 }
8084
8185 #attachSubscriptionCleanupHandlers( win : BrowserWindow ) {
86+ const webContentsId = win . webContents . id ;
8287 win . webContents . on ( 'did-start-navigation' , ( { isSameDocument, frame } ) => {
8388 // Check if it's a hard navigation
8489 if ( ! isSameDocument ) {
8590 debug (
8691 'Handling hard navigation event' ,
87- `webContentsId: ${ win . webContents . id } ` ,
92+ `webContentsId: ${ webContentsId } ` ,
8893 `frameRoutingId: ${ frame . routingId } `
8994 ) ;
9095 this . #cleanUpSubscriptions( {
91- webContentsId : win . webContents . id ,
96+ webContentsId : webContentsId ,
9297 frameRoutingId : frame . routingId ,
9398 } ) ;
9499 }
95100 } ) ;
96101 win . webContents . on ( 'destroyed' , ( ) => {
97102 debug ( 'Handling webContents `destroyed` event' ) ;
98- this . detachWindow ( win ) ;
103+ this . detachWindow ( win , webContentsId ) ;
99104 } ) ;
100105 }
101106}
0 commit comments