Skip to content

Commit

Permalink
chore: fix the errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Apr 23, 2024
1 parent 8dfbffb commit 7ba043c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions packages/puppeteer-core/src/bidi/CDPSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class BidiCdpSession extends CDPSession {
static sessions = new Map<string, BidiCdpSession>();

#detached = false;
readonly #connection: BidiConnection | undefined = undefined;
readonly #connection?: BidiConnection;
readonly #sessionId = Deferred.create<string>();
readonly frame: BidiFrame;

Expand All @@ -41,11 +41,11 @@ export class BidiCdpSession extends CDPSession {
} else {
(async () => {
try {
const session = await connection.send('cdp.getSession', {
const {result} = await connection.send('cdp.getSession', {
context: frame._id,
});
this.#sessionId.resolve(session.result.session!);
BidiCdpSession.sessions.set(session.result.session!, this);
this.#sessionId.resolve(result.session!);
BidiCdpSession.sessions.set(result.session!, this);
} catch (error) {
this.#sessionId.reject(error as Error);
}
Expand Down Expand Up @@ -89,13 +89,29 @@ export class BidiCdpSession extends CDPSession {
}

override async detach(): Promise<void> {
if (this.#connection === undefined || this.#detached) {
if (
this.#connection === undefined ||
this.#connection.closed ||
this.#detached
) {
return;
}
try {
await this.frame.client.send('Target.detachFromTarget', {
sessionId: this.id(),
});
} finally {
this.onClose();
}
}

/**
* @internal
*/
onClose = (): void => {
BidiCdpSession.sessions.delete(this.id());
this.#detached = true;
}
};

override id(): string {
const value = this.#sessionId.value();
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/bidi/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class BidiFrame extends Frame {
this.browsingContext.on('closed', () => {
for (const session of BidiCdpSession.sessions.values()) {
if (session.frame === this) {
void session.detach().catch(debugError);
session.onClose();
}
}
this.page().trustedEmitter.emit(PageEvent.FrameDetached, this);
Expand Down

0 comments on commit 7ba043c

Please sign in to comment.