@@ -59,6 +59,7 @@ export class WKPage implements PageDelegate {
59
59
this . _page = new Page ( this , browserContext ) ;
60
60
this . _workers = new WKWorkers ( this . _page ) ;
61
61
this . _session = undefined as any as WKSession ;
62
+ this . _page . on ( Events . Page . FrameDetached , frame => this . _removeContextsForFrame ( frame , false ) ) ;
62
63
}
63
64
64
65
private async _initializePageProxySession ( ) {
@@ -248,13 +249,8 @@ export class WKPage implements PageDelegate {
248
249
249
250
private _onFrameNavigated ( framePayload : Protocol . Page . Frame , initial : boolean ) {
250
251
const frame = this . _page . _frameManager . frame ( framePayload . id ) ;
251
- for ( const [ contextId , context ] of this . _contextIdToContext ) {
252
- if ( context . frame === frame ) {
253
- ( context . _delegate as WKExecutionContext ) . _dispose ( ) ;
254
- this . _contextIdToContext . delete ( contextId ) ;
255
- frame . _contextDestroyed ( context ) ;
256
- }
257
- }
252
+ assert ( frame ) ;
253
+ this . _removeContextsForFrame ( frame ! , true ) ;
258
254
if ( ! framePayload . parentId )
259
255
this . _workers . clear ( ) ;
260
256
this . _page . _frameManager . frameCommittedNewDocumentNavigation ( framePayload . id , framePayload . url , framePayload . name || '' , framePayload . loaderId , initial ) ;
@@ -268,6 +264,17 @@ export class WKPage implements PageDelegate {
268
264
this . _page . _frameManager . frameDetached ( frameId ) ;
269
265
}
270
266
267
+ private _removeContextsForFrame ( frame : frames . Frame , notifyFrame : boolean ) {
268
+ for ( const [ contextId , context ] of this . _contextIdToContext ) {
269
+ if ( context . frame === frame ) {
270
+ ( context . _delegate as WKExecutionContext ) . _dispose ( ) ;
271
+ this . _contextIdToContext . delete ( contextId ) ;
272
+ if ( notifyFrame )
273
+ frame . _contextDestroyed ( context ) ;
274
+ }
275
+ }
276
+ }
277
+
271
278
private _onExecutionContextCreated ( contextPayload : Protocol . Runtime . ExecutionContextDescription ) {
272
279
if ( this . _contextIdToContext . has ( contextPayload . id ) )
273
280
return ;
0 commit comments