Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move disposed handling #12405

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/puppeteer-core/src/cdp/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -502,7 +500,6 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
return;
}
this.#contextIdToContext.delete(key);
context._world.clearContext();
});
}

Expand Down
17 changes: 8 additions & 9 deletions packages/puppeteer-core/src/cdp/IsolatedWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down