chore: small fixes and type improvement (#11110)

This commit is contained in:
Nikolay Vitkov 2023-10-10 11:42:31 +02:00 committed by GitHub
parent 067a5b2900
commit 2811119562
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 13 deletions

View File

@ -80,7 +80,7 @@ class CdpConnectionAdapter {
getCdpClient(id: string) { getCdpClient(id: string) {
const session = this.#cdp.session(id); const session = this.#cdp.session(id);
if (!session) { 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)) { if (!this.#adapters.has(session)) {
const adapter = new CDPClientAdapter(session, id, this.#browser); const adapter = new CDPClientAdapter(session, id, this.#browser);

View File

@ -92,7 +92,7 @@ export class BidiPage extends Page {
#frameTree = new FrameTree<BidiFrame>(); #frameTree = new FrameTree<BidiFrame>();
#networkManager: BidiNetworkManager; #networkManager: BidiNetworkManager;
#viewport: Viewport | null = null; #viewport: Viewport | null = null;
#closedDeferred = Deferred.create<TargetCloseError>(); #closedDeferred = Deferred.create<never, TargetCloseError>();
#subscribedEvents = new Map<Bidi.Event['method'], Handler<any>>([ #subscribedEvents = new Map<Bidi.Event['method'], Handler<any>>([
['log.entryAdded', this.#onLogEntryAdded.bind(this)], ['log.entryAdded', this.#onLogEntryAdded.bind(this)],
['browsingContext.load', this.#onFrameLoaded.bind(this)], ['browsingContext.load', this.#onFrameLoaded.bind(this)],
@ -474,7 +474,7 @@ export class BidiPage extends Page {
return; return;
} }
this.#closedDeferred.resolve(new TargetCloseError('Page closed!')); this.#closedDeferred.reject(new TargetCloseError('Page closed!'));
this.#networkManager.dispose(); this.#networkManager.dispose();
await this.#connection.send('browsingContext.close', { await this.#connection.send('browsingContext.close', {

View File

@ -133,7 +133,7 @@ export class CdpPage extends Page {
#viewport: Viewport | null; #viewport: Viewport | null;
#workers = new Map<string, WebWorker>(); #workers = new Map<string, WebWorker>();
#fileChooserDeferreds = new Set<Deferred<FileChooser>>(); #fileChooserDeferreds = new Set<Deferred<FileChooser>>();
#sessionCloseDeferred = Deferred.create<TargetCloseError>(); #sessionCloseDeferred = Deferred.create<never, TargetCloseError>();
#serviceWorkerBypassed = false; #serviceWorkerBypassed = false;
#userDragInterceptionEnabled = false; #userDragInterceptionEnabled = false;
@ -195,7 +195,7 @@ export class CdpPage extends Page {
[ [
CDPSessionEvent.Disconnected, CDPSessionEvent.Disconnected,
() => { () => {
this.#sessionCloseDeferred.resolve( this.#sessionCloseDeferred.reject(
new TargetCloseError('Target closed') new TargetCloseError('Target closed')
); );
}, },

View File

@ -17,26 +17,28 @@ export interface DeferredOptions {
* *
* @internal * @internal
*/ */
export class Deferred<T> { export class Deferred<T, V extends Error = Error> {
#isResolved = false; #isResolved = false;
#isRejected = false; #isRejected = false;
#value: T | Error | undefined; #value: T | V | TimeoutError | undefined;
#resolver: (value: void) => void = () => {}; #resolver: (value: void) => void = () => {};
#taskPromise = new Promise<void>(resolve => { #taskPromise = new Promise<void>(resolve => {
this.#resolver = resolve; this.#resolver = resolve;
}); });
#timeoutId: ReturnType<typeof setTimeout> | undefined; #timeoutId: ReturnType<typeof setTimeout> | undefined;
#timeoutError: TimeoutError;
constructor(opts?: DeferredOptions) { constructor(opts?: DeferredOptions) {
this.#timeoutError = new TimeoutError(opts && opts.message);
this.#timeoutId = this.#timeoutId =
opts && opts.timeout > 0 opts && opts.timeout > 0
? setTimeout(() => { ? setTimeout(() => {
this.reject(new TimeoutError(opts.message)); this.reject(this.#timeoutError);
}, opts.timeout) }, opts.timeout)
: undefined; : undefined;
} }
#finish(value: T | Error) { #finish(value: T | V | TimeoutError) {
clearTimeout(this.#timeoutId); clearTimeout(this.#timeoutId);
this.#value = value; this.#value = value;
this.#resolver(); this.#resolver();
@ -50,7 +52,7 @@ export class Deferred<T> {
this.#finish(value); this.#finish(value);
} }
reject(error: Error): void { reject(error: V | TimeoutError): void {
if (this.#isRejected || this.#isResolved) { if (this.#isRejected || this.#isResolved) {
return; return;
} }
@ -66,7 +68,7 @@ export class Deferred<T> {
return this.#isResolved || this.#isRejected; return this.#isResolved || this.#isRejected;
} }
value(): T | Error | undefined { value(): T | V | TimeoutError | undefined {
return this.#value; return this.#value;
} }
@ -78,8 +80,10 @@ export class Deferred<T> {
return this.#value as T; return this.#value as T;
} }
static create<R>(opts?: DeferredOptions): Deferred<R> { static create<R, X extends Error = Error>(
return new Deferred<R>(opts); opts?: DeferredOptions
): Deferred<R> {
return new Deferred<R, X>(opts);
} }
static async race<R>( static async race<R>(