chore: fix strict-mode TS in LifecycleWatcher (#8387)

This commit is contained in:
Alex Rudenko 2022-05-24 13:14:19 +02:00 committed by GitHub
parent a8a08a8441
commit b349d71dcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,6 +54,8 @@ const puppeteerToProtocolLifecycle = new Map<
['networkidle2', 'networkAlmostIdle'], ['networkidle2', 'networkAlmostIdle'],
]); ]);
const noop = (): void => {};
/** /**
* @internal * @internal
*/ */
@ -62,23 +64,33 @@ export class LifecycleWatcher {
_frameManager: FrameManager; _frameManager: FrameManager;
_frame: Frame; _frame: Frame;
_timeout: number; _timeout: number;
_navigationRequest?: HTTPRequest; _navigationRequest: HTTPRequest | null = null;
_eventListeners: PuppeteerEventListener[]; _eventListeners: PuppeteerEventListener[];
_initialLoaderId: string; _initialLoaderId: string;
_sameDocumentNavigationPromise: Promise<Error | null>; _sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
_sameDocumentNavigationCompleteCallback: (x?: Error) => void; _sameDocumentNavigationPromise = new Promise<Error | undefined>((fulfill) => {
this._sameDocumentNavigationCompleteCallback = fulfill;
});
_lifecyclePromise: Promise<void>; _lifecycleCallback: () => void = noop;
_lifecycleCallback: () => void; _lifecyclePromise: Promise<void> = new Promise((fulfill) => {
this._lifecycleCallback = fulfill;
});
_newDocumentNavigationPromise: Promise<Error | null>; _newDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
_newDocumentNavigationCompleteCallback: (x?: Error) => void; _newDocumentNavigationPromise: Promise<Error | undefined> = new Promise(
(fulfill) => {
this._newDocumentNavigationCompleteCallback = fulfill;
}
);
_terminationPromise: Promise<Error | null>; _terminationCallback: (x?: Error) => void = noop;
_terminationCallback: (x?: Error) => void; _terminationPromise: Promise<Error | undefined> = new Promise((fulfill) => {
this._terminationCallback = fulfill;
});
_timeoutPromise: Promise<TimeoutError | null>; _timeoutPromise: Promise<TimeoutError | undefined>;
_maximumTimer?: NodeJS.Timeout; _maximumTimer?: NodeJS.Timeout;
_hasSameDocumentNavigation?: boolean; _hasSameDocumentNavigation?: boolean;
@ -95,14 +107,13 @@ export class LifecycleWatcher {
this._expectedLifecycle = waitUntil.map((value) => { this._expectedLifecycle = waitUntil.map((value) => {
const protocolEvent = puppeteerToProtocolLifecycle.get(value); const protocolEvent = puppeteerToProtocolLifecycle.get(value);
assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value); assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value);
return protocolEvent; return protocolEvent as ProtocolLifeCycleEvent;
}); });
this._frameManager = frameManager; this._frameManager = frameManager;
this._frame = frame; this._frame = frame;
this._initialLoaderId = frame._loaderId; this._initialLoaderId = frame._loaderId;
this._timeout = timeout; this._timeout = timeout;
this._navigationRequest = null;
this._eventListeners = [ this._eventListeners = [
helper.addEventListener( helper.addEventListener(
frameManager._client, frameManager._client,
@ -139,24 +150,7 @@ export class LifecycleWatcher {
), ),
]; ];
this._sameDocumentNavigationPromise = new Promise<Error | null>(
(fulfill) => {
this._sameDocumentNavigationCompleteCallback = fulfill;
}
);
this._lifecyclePromise = new Promise((fulfill) => {
this._lifecycleCallback = fulfill;
});
this._newDocumentNavigationPromise = new Promise((fulfill) => {
this._newDocumentNavigationCompleteCallback = fulfill;
});
this._timeoutPromise = this._createTimeoutPromise(); this._timeoutPromise = this._createTimeoutPromise();
this._terminationPromise = new Promise((fulfill) => {
this._terminationCallback = fulfill;
});
this._checkLifecycleComplete(); this._checkLifecycleComplete();
} }
@ -186,11 +180,11 @@ export class LifecycleWatcher {
this._terminationCallback.call(null, error); this._terminationCallback.call(null, error);
} }
sameDocumentNavigationPromise(): Promise<Error | null> { sameDocumentNavigationPromise(): Promise<Error | undefined> {
return this._sameDocumentNavigationPromise; return this._sameDocumentNavigationPromise;
} }
newDocumentNavigationPromise(): Promise<Error | null> { newDocumentNavigationPromise(): Promise<Error | undefined> {
return this._newDocumentNavigationPromise; return this._newDocumentNavigationPromise;
} }
@ -198,11 +192,11 @@ export class LifecycleWatcher {
return this._lifecyclePromise; return this._lifecyclePromise;
} }
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | null> { timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
return Promise.race([this._timeoutPromise, this._terminationPromise]); return Promise.race([this._timeoutPromise, this._terminationPromise]);
} }
_createTimeoutPromise(): Promise<TimeoutError | null> { _createTimeoutPromise(): Promise<TimeoutError | undefined> {
if (!this._timeout) return new Promise(() => {}); if (!this._timeout) return new Promise(() => {});
const errorMessage = const errorMessage =
'Navigation timeout of ' + this._timeout + ' ms exceeded'; 'Navigation timeout of ' + this._timeout + ' ms exceeded';
@ -268,6 +262,6 @@ export class LifecycleWatcher {
dispose(): void { dispose(): void {
helper.removeEventListeners(this._eventListeners); helper.removeEventListeners(this._eventListeners);
clearTimeout(this._maximumTimer); this._maximumTimer !== undefined && clearTimeout(this._maximumTimer);
} }
} }