@@ -55,11 +55,15 @@ class IPCHandler<TRouter extends AnyRouter> {
55
55
this . #attachSubscriptionCleanupHandlers( win ) ;
56
56
}
57
57
58
- detachWindow ( win : BrowserWindow ) {
58
+ detachWindow ( win : BrowserWindow , webContentsId ?: number ) {
59
59
debug ( 'Detaching window' , win . id ) ;
60
60
61
+ if ( win . isDestroyed ( ) && webContentsId === undefined ) {
62
+ throw new Error ( 'webContentsId is required when calling detachWindow on a destroyed window' ) ;
63
+ }
64
+
61
65
this . #windows = this . #windows. filter ( ( w ) => w !== win ) ;
62
- this . #cleanUpSubscriptions( { webContentsId : win . webContents . id } ) ;
66
+ this . #cleanUpSubscriptions( { webContentsId : webContentsId ?? win . webContents . id } ) ;
63
67
}
64
68
65
69
#cleanUpSubscriptions( {
@@ -79,23 +83,24 @@ class IPCHandler<TRouter extends AnyRouter> {
79
83
}
80
84
81
85
#attachSubscriptionCleanupHandlers( win : BrowserWindow ) {
86
+ const webContentsId = win . webContents . id ;
82
87
win . webContents . on ( 'did-start-navigation' , ( { isSameDocument, frame } ) => {
83
88
// Check if it's a hard navigation
84
89
if ( ! isSameDocument ) {
85
90
debug (
86
91
'Handling hard navigation event' ,
87
- `webContentsId: ${ win . webContents . id } ` ,
92
+ `webContentsId: ${ webContentsId } ` ,
88
93
`frameRoutingId: ${ frame . routingId } `
89
94
) ;
90
95
this . #cleanUpSubscriptions( {
91
- webContentsId : win . webContents . id ,
96
+ webContentsId : webContentsId ,
92
97
frameRoutingId : frame . routingId ,
93
98
} ) ;
94
99
}
95
100
} ) ;
96
101
win . webContents . on ( 'destroyed' , ( ) => {
97
102
debug ( 'Handling webContents `destroyed` event' ) ;
98
- this . detachWindow ( win ) ;
103
+ this . detachWindow ( win , webContentsId ) ;
99
104
} ) ;
100
105
}
101
106
}
0 commit comments