fix: restore deferred promise debugging (#8895)

* fix: restore deferred promise debugging

* chore: strict types
This commit is contained in:
Alex Rudenko 2022-09-05 10:30:47 +02:00 committed by GitHub
parent f9e758e31d
commit 7b42250c7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 37 deletions

View File

@ -16,10 +16,8 @@
import {Protocol} from 'devtools-protocol'; import {Protocol} from 'devtools-protocol';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
import { import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js';
createDeferredPromise, import {DeferredPromise} from '../util/DeferredPromise.js';
DeferredPromise,
} from '../util/DeferredPromise.js';
import {isErrorLike} from '../util/ErrorLike.js'; import {isErrorLike} from '../util/ErrorLike.js';
import {CDPSession} from './Connection.js'; import {CDPSession} from './Connection.js';
import {EventEmitter} from './EventEmitter.js'; import {EventEmitter} from './EventEmitter.js';
@ -150,9 +148,9 @@ export class FrameManager extends EventEmitter {
if (!this.#framesPendingTargetInit.has(targetId)) { if (!this.#framesPendingTargetInit.has(targetId)) {
this.#framesPendingTargetInit.set( this.#framesPendingTargetInit.set(
targetId, targetId,
createDeferredPromise({ createDebuggableDeferredPromise(
message: `Waiting for target frame ${targetId} failed`, `Waiting for target frame ${targetId} failed`
}) )
); );
} }
const result = await Promise.all([ const result = await Promise.all([
@ -318,9 +316,9 @@ export class FrameManager extends EventEmitter {
if (!this.#framesPendingAttachment.has(frameId)) { if (!this.#framesPendingAttachment.has(frameId)) {
this.#framesPendingAttachment.set( this.#framesPendingAttachment.set(
frameId, frameId,
createDeferredPromise({ createDebuggableDeferredPromise(
message: `Waiting for frame ${frameId} to attach failed`, `Waiting for frame ${frameId} to attach failed`
}) )
); );
} }
frame.then(() => { frame.then(() => {

View File

@ -23,10 +23,8 @@ import {HTTPRequest} from './HTTPRequest.js';
import {HTTPResponse} from './HTTPResponse.js'; import {HTTPResponse} from './HTTPResponse.js';
import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js'; import {FetchRequestId, NetworkEventManager} from './NetworkEventManager.js';
import {debugError, isString} from './util.js'; import {debugError, isString} from './util.js';
import { import {DeferredPromise} from '../util/DeferredPromise.js';
createDeferredPromise, import {createDebuggableDeferredPromise} from '../util/DebuggableDeferredPromise.js';
DeferredPromise,
} from '../util/DeferredPromise.js';
/** /**
* @public * @public
@ -144,9 +142,9 @@ export class NetworkManager extends EventEmitter {
if (this.#deferredInitPromise) { if (this.#deferredInitPromise) {
return this.#deferredInitPromise; return this.#deferredInitPromise;
} }
this.#deferredInitPromise = createDeferredPromise<void>({ this.#deferredInitPromise = createDebuggableDeferredPromise(
message: 'NetworkManager initialization timed out', 'NetworkManager initialization timed out'
}); );
const init = Promise.all([ const init = Promise.all([
this.#ignoreHTTPSErrors this.#ignoreHTTPSErrors
? this.#client.send('Security.setIgnoreCertificateErrors', { ? this.#client.send('Security.setIgnoreCertificateErrors', {

View File

@ -68,6 +68,7 @@ export * from './node/util.js';
export * from './puppeteer.js'; export * from './puppeteer.js';
export * from './revisions.js'; export * from './revisions.js';
export * from './util/assert.js'; export * from './util/assert.js';
export * from './util/DebuggableDeferredPromise.js';
export * from './util/DeferredPromise.js'; export * from './util/DeferredPromise.js';
export * from './util/ErrorLike.js'; export * from './util/ErrorLike.js';
export * from './util/getPackageDirectory.js'; export * from './util/getPackageDirectory.js';

View File

@ -0,0 +1,20 @@
import {DEFERRED_PROMISE_DEBUG_TIMEOUT} from '../environment.js';
import {DeferredPromise, createDeferredPromise} from './DeferredPromise.js';
/**
* Creates and returns a deferred promise using DEFERRED_PROMISE_DEBUG_TIMEOUT
* if it's specified or a normal deferred promise otherwise.
*
* @internal
*/
export function createDebuggableDeferredPromise<T>(
message: string
): DeferredPromise<T> {
if (DEFERRED_PROMISE_DEBUG_TIMEOUT > 0) {
return createDeferredPromise({
message,
timeout: DEFERRED_PROMISE_DEBUG_TIMEOUT,
});
}
return createDeferredPromise();
}

View File

@ -1,5 +1,4 @@
import {TimeoutError} from '../common/Errors.js'; import {TimeoutError} from '../common/Errors.js';
import {DEFERRED_PROMISE_DEBUG_TIMEOUT} from '../environment.js';
/** /**
* @internal * @internal
@ -11,27 +10,26 @@ export interface DeferredPromise<T> extends Promise<T> {
reject: (_: Error) => void; reject: (_: Error) => void;
} }
interface DeferredPromiseOptions { /**
message?: string; * @internal
timeout?: number; */
isDebug?: boolean; export interface DeferredPromiseOptions {
message: string;
timeout: number;
} }
/** /**
* Creates an returns a promise along with the resolve/reject functions. * Creates and returns a promise along with the resolve/reject functions.
* *
* If the promise has not been resolved/rejected within the `timeout` period, * If the promise has not been resolved/rejected within the `timeout` period,
* the promise gets rejected with a timeout error. * the promise gets rejected with a timeout error. `timeout` has to be greater than 0 or
* it is ignored.
* *
* @internal * @internal
*/ */
export function createDeferredPromise<T>({ export function createDeferredPromise<T>(
message, opts?: DeferredPromiseOptions
timeout = 5000, ): DeferredPromise<T> {
}: DeferredPromiseOptions = {}): DeferredPromise<T> {
if (DEFERRED_PROMISE_DEBUG_TIMEOUT > 0 && !timeout) {
timeout = DEFERRED_PROMISE_DEBUG_TIMEOUT;
}
let isResolved = false; let isResolved = false;
let isRejected = false; let isRejected = false;
let resolver = (_: T): void => {}; let resolver = (_: T): void => {};
@ -40,12 +38,13 @@ export function createDeferredPromise<T>({
resolver = resolve; resolver = resolve;
rejector = reject; rejector = reject;
}); });
const timeoutId = message const timeoutId =
? setTimeout(() => { opts && opts.timeout > 0
isRejected = true; ? setTimeout(() => {
rejector(new TimeoutError(message)); isRejected = true;
}, timeout) rejector(new TimeoutError(opts.message));
: undefined; }, opts.timeout)
: undefined;
return Object.assign(taskPromise, { return Object.assign(taskPromise, {
resolved: () => { resolved: () => {
return isResolved; return isResolved;