refactor: move disposed handling (#12405)

This commit is contained in:
Alex Rudenko 2024-05-07 11:01:48 +02:00 committed by GitHub
parent 4937025fb3
commit 93bf52bdfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 13 deletions

View File

@ -490,9 +490,7 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
contextPayload,
world
);
if (world) {
world.setContext(context);
}
world.setContext(context);
const key = `${session.id()}:${contextPayload.id}`;
this.#contextIdToContext.set(key, context);
context.once('disposed', () => {
@ -502,7 +500,6 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
return;
}
this.#contextIdToContext.delete(key);
context._world.clearContext();
});
}

View File

@ -64,20 +64,19 @@ export class IsolatedWorld extends Realm {
return this.#frameOrWorker.client;
}
clearContext(): void {
// The message has to match the CDP message expected by the WaitTask class.
this.#context?.reject(new Error('Execution context was destroyed'));
this.#context = Deferred.create();
if ('clearDocumentHandle' in this.#frameOrWorker) {
this.#frameOrWorker.clearDocumentHandle();
}
}
setContext(context: ExecutionContext): void {
const existingContext = this.#context.value();
if (existingContext instanceof ExecutionContext) {
existingContext[disposeSymbol]();
}
context.once('disposed', () => {
// The message has to match the CDP message expected by the WaitTask class.
this.#context?.reject(new Error('Execution context was destroyed'));
this.#context = Deferred.create();
if ('clearDocumentHandle' in this.#frameOrWorker) {
this.#frameOrWorker.clearDocumentHandle();
}
});
this.#context.resolve(context);
void this.taskManager.rerunAll();
}