chore: don't detach CdpSession (#12154)

This commit is contained in:
Nikolay Vitkov 2024-04-23 10:49:35 +02:00 committed by GitHub
parent ed9d7dd2f5
commit f2391307d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 8 deletions

View File

@ -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;
@ -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);
}
@ -89,7 +89,11 @@ 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 {
@ -97,11 +101,18 @@ export class BidiCdpSession extends CDPSession {
sessionId: this.id(),
});
} finally {
BidiCdpSession.sessions.delete(this.id());
this.#detached = true;
this.onClose();
}
}
/**
* @internal
*/
onClose = (): void => {
BidiCdpSession.sessions.delete(this.id());
this.#detached = true;
};
override id(): string {
const value = this.#sessionId.value();
return typeof value === 'string' ? value : '';

View File

@ -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);