mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: small fixes and type improvement (#11110)
This commit is contained in:
parent
067a5b2900
commit
2811119562
@ -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);
|
||||
|
@ -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', {
|
||||
|
@ -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')
|
||||
);
|
||||
},
|
||||
|
@ -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>(
|
||||
|
Loading…
Reference in New Issue
Block a user