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

View File

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

View File

@ -68,6 +68,7 @@ export * from './node/util.js';
export * from './puppeteer.js';
export * from './revisions.js';
export * from './util/assert.js';
export * from './util/DebuggableDeferredPromise.js';
export * from './util/DeferredPromise.js';
export * from './util/ErrorLike.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 {DEFERRED_PROMISE_DEBUG_TIMEOUT} from '../environment.js';
/**
* @internal
@ -11,27 +10,26 @@ export interface DeferredPromise<T> extends Promise<T> {
reject: (_: Error) => void;
}
interface DeferredPromiseOptions {
message?: string;
timeout?: number;
isDebug?: boolean;
/**
* @internal
*/
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,
* 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
*/
export function createDeferredPromise<T>({
message,
timeout = 5000,
}: DeferredPromiseOptions = {}): DeferredPromise<T> {
if (DEFERRED_PROMISE_DEBUG_TIMEOUT > 0 && !timeout) {
timeout = DEFERRED_PROMISE_DEBUG_TIMEOUT;
}
export function createDeferredPromise<T>(
opts?: DeferredPromiseOptions
): DeferredPromise<T> {
let isResolved = false;
let isRejected = false;
let resolver = (_: T): void => {};
@ -40,12 +38,13 @@ export function createDeferredPromise<T>({
resolver = resolve;
rejector = reject;
});
const timeoutId = message
? setTimeout(() => {
isRejected = true;
rejector(new TimeoutError(message));
}, timeout)
: undefined;
const timeoutId =
opts && opts.timeout > 0
? setTimeout(() => {
isRejected = true;
rejector(new TimeoutError(opts.message));
}, opts.timeout)
: undefined;
return Object.assign(taskPromise, {
resolved: () => {
return isResolved;