Skip to content

Commit

Permalink
chore: cache executionContextID on Realm
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Mar 22, 2024
1 parent 22a680d commit 8ac6a18
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/puppeteer-core/src/bidi/core/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export abstract class Realm extends EventEmitter<{
protected readonly disposables = new DisposableStack();
readonly id: string;
readonly origin: string;
protected executionContextId?: number;
// keep-sorted end

protected constructor(id: string, origin: string) {
Expand Down Expand Up @@ -127,11 +128,15 @@ export abstract class Realm extends EventEmitter<{
return realm.#reason!;
})
async resolveExecutionContextId(): Promise<number> {
const {result} = await (this.session.connection as BidiConnection).send(
'cdp.resolveRealm',
{realm: this.id}
);
return result.executionContextId;
if (!this.executionContextId) {
const {result} = await (this.session.connection as BidiConnection).send(
'cdp.resolveRealm',
{realm: this.id}
);
this.executionContextId = result.executionContextId;
}

return this.executionContextId;
}

[disposeSymbol](): void {
Expand Down Expand Up @@ -188,6 +193,7 @@ export class WindowRealm extends Realm {
}
(this as any).id = info.realm;
(this as any).origin = info.origin;
this.executionContextId = undefined;
this.emit('updated', this);
});
sessionEmitter.on('script.realmCreated', info => {
Expand Down

0 comments on commit 8ac6a18

Please sign in to comment.