From 28111195627541772285056763c1256636f31ff9 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Tue, 10 Oct 2023 11:42:31 +0200 Subject: [PATCH] chore: small fixes and type improvement (#11110) --- .../puppeteer-core/src/bidi/BidiOverCdp.ts | 2 +- packages/puppeteer-core/src/bidi/Page.ts | 4 ++-- packages/puppeteer-core/src/cdp/Page.ts | 4 ++-- packages/puppeteer-core/src/util/Deferred.ts | 20 +++++++++++-------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/puppeteer-core/src/bidi/BidiOverCdp.ts b/packages/puppeteer-core/src/bidi/BidiOverCdp.ts index 0083eee9..f6f262b6 100644 --- a/packages/puppeteer-core/src/bidi/BidiOverCdp.ts +++ b/packages/puppeteer-core/src/bidi/BidiOverCdp.ts @@ -80,7 +80,7 @@ class CdpConnectionAdapter { getCdpClient(id: string) { const session = this.#cdp.session(id); if (!session) { - throw new Error('Unknown CDP session with id' + id); + throw new Error(`Unknown CDP session with id ${id}`); } if (!this.#adapters.has(session)) { const adapter = new CDPClientAdapter(session, id, this.#browser); diff --git a/packages/puppeteer-core/src/bidi/Page.ts b/packages/puppeteer-core/src/bidi/Page.ts index 0601c976..31247c79 100644 --- a/packages/puppeteer-core/src/bidi/Page.ts +++ b/packages/puppeteer-core/src/bidi/Page.ts @@ -92,7 +92,7 @@ export class BidiPage extends Page { #frameTree = new FrameTree(); #networkManager: BidiNetworkManager; #viewport: Viewport | null = null; - #closedDeferred = Deferred.create(); + #closedDeferred = Deferred.create(); #subscribedEvents = new Map>([ ['log.entryAdded', this.#onLogEntryAdded.bind(this)], ['browsingContext.load', this.#onFrameLoaded.bind(this)], @@ -474,7 +474,7 @@ export class BidiPage extends Page { return; } - this.#closedDeferred.resolve(new TargetCloseError('Page closed!')); + this.#closedDeferred.reject(new TargetCloseError('Page closed!')); this.#networkManager.dispose(); await this.#connection.send('browsingContext.close', { diff --git a/packages/puppeteer-core/src/cdp/Page.ts b/packages/puppeteer-core/src/cdp/Page.ts index 8379d64c..dcdb7461 100644 --- a/packages/puppeteer-core/src/cdp/Page.ts +++ b/packages/puppeteer-core/src/cdp/Page.ts @@ -133,7 +133,7 @@ export class CdpPage extends Page { #viewport: Viewport | null; #workers = new Map(); #fileChooserDeferreds = new Set>(); - #sessionCloseDeferred = Deferred.create(); + #sessionCloseDeferred = Deferred.create(); #serviceWorkerBypassed = false; #userDragInterceptionEnabled = false; @@ -195,7 +195,7 @@ export class CdpPage extends Page { [ CDPSessionEvent.Disconnected, () => { - this.#sessionCloseDeferred.resolve( + this.#sessionCloseDeferred.reject( new TargetCloseError('Target closed') ); }, diff --git a/packages/puppeteer-core/src/util/Deferred.ts b/packages/puppeteer-core/src/util/Deferred.ts index 9c418f46..47184aa2 100644 --- a/packages/puppeteer-core/src/util/Deferred.ts +++ b/packages/puppeteer-core/src/util/Deferred.ts @@ -17,26 +17,28 @@ export interface DeferredOptions { * * @internal */ -export class Deferred { +export class Deferred { #isResolved = false; #isRejected = false; - #value: T | Error | undefined; + #value: T | V | TimeoutError | undefined; #resolver: (value: void) => void = () => {}; #taskPromise = new Promise(resolve => { this.#resolver = resolve; }); #timeoutId: ReturnType | undefined; + #timeoutError: TimeoutError; constructor(opts?: DeferredOptions) { + this.#timeoutError = new TimeoutError(opts && opts.message); this.#timeoutId = opts && opts.timeout > 0 ? setTimeout(() => { - this.reject(new TimeoutError(opts.message)); + this.reject(this.#timeoutError); }, opts.timeout) : undefined; } - #finish(value: T | Error) { + #finish(value: T | V | TimeoutError) { clearTimeout(this.#timeoutId); this.#value = value; this.#resolver(); @@ -50,7 +52,7 @@ export class Deferred { this.#finish(value); } - reject(error: Error): void { + reject(error: V | TimeoutError): void { if (this.#isRejected || this.#isResolved) { return; } @@ -66,7 +68,7 @@ export class Deferred { return this.#isResolved || this.#isRejected; } - value(): T | Error | undefined { + value(): T | V | TimeoutError | undefined { return this.#value; } @@ -78,8 +80,10 @@ export class Deferred { return this.#value as T; } - static create(opts?: DeferredOptions): Deferred { - return new Deferred(opts); + static create( + opts?: DeferredOptions + ): Deferred { + return new Deferred(opts); } static async race(