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

View File

@ -92,7 +92,7 @@ export class BidiPage extends Page {
#frameTree = new FrameTree<BidiFrame>();
#networkManager: BidiNetworkManager;
#viewport: Viewport | null = null;
#closedDeferred = Deferred.create<TargetCloseError>();
#closedDeferred = Deferred.create<never, TargetCloseError>();
#subscribedEvents = new Map<Bidi.Event['method'], Handler<any>>([
['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', {

View File

@ -133,7 +133,7 @@ export class CdpPage extends Page {
#viewport: Viewport | null;
#workers = new Map<string, WebWorker>();
#fileChooserDeferreds = new Set<Deferred<FileChooser>>();
#sessionCloseDeferred = Deferred.create<TargetCloseError>();
#sessionCloseDeferred = Deferred.create<never, TargetCloseError>();
#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')
);
},

View File

@ -17,26 +17,28 @@ export interface DeferredOptions {
*
* @internal
*/
export class Deferred<T> {
export class Deferred<T, V extends Error = Error> {
#isResolved = false;
#isRejected = false;
#value: T | Error | undefined;
#value: T | V | TimeoutError | undefined;
#resolver: (value: void) => void = () => {};
#taskPromise = new Promise<void>(resolve => {
this.#resolver = resolve;
});
#timeoutId: ReturnType<typeof setTimeout> | 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<T> {
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<T> {
return this.#isResolved || this.#isRejected;
}
value(): T | Error | undefined {
value(): T | V | TimeoutError | undefined {
return this.#value;
}
@ -78,8 +80,10 @@ export class Deferred<T> {
return this.#value as T;
}
static create<R>(opts?: DeferredOptions): Deferred<R> {
return new Deferred<R>(opts);
static create<R, X extends Error = Error>(
opts?: DeferredOptions
): Deferred<R> {
return new Deferred<R, X>(opts);
}
static async race<R>(