From b349d71dcd33b3092cb0d27b52c44b440ad18279 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 24 May 2022 13:14:19 +0200 Subject: [PATCH] chore: fix strict-mode TS in LifecycleWatcher (#8387) --- src/common/LifecycleWatcher.ts | 62 +++++++++++++++------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/src/common/LifecycleWatcher.ts b/src/common/LifecycleWatcher.ts index 276ddde42ed8b..16f2b94d7c2ec 100644 --- a/src/common/LifecycleWatcher.ts +++ b/src/common/LifecycleWatcher.ts @@ -54,6 +54,8 @@ const puppeteerToProtocolLifecycle = new Map< ['networkidle2', 'networkAlmostIdle'], ]); +const noop = (): void => {}; + /** * @internal */ @@ -62,23 +64,33 @@ export class LifecycleWatcher { _frameManager: FrameManager; _frame: Frame; _timeout: number; - _navigationRequest?: HTTPRequest; + _navigationRequest: HTTPRequest | null = null; _eventListeners: PuppeteerEventListener[]; _initialLoaderId: string; - _sameDocumentNavigationPromise: Promise; - _sameDocumentNavigationCompleteCallback: (x?: Error) => void; + _sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop; + _sameDocumentNavigationPromise = new Promise((fulfill) => { + this._sameDocumentNavigationCompleteCallback = fulfill; + }); - _lifecyclePromise: Promise; - _lifecycleCallback: () => void; + _lifecycleCallback: () => void = noop; + _lifecyclePromise: Promise = new Promise((fulfill) => { + this._lifecycleCallback = fulfill; + }); - _newDocumentNavigationPromise: Promise; - _newDocumentNavigationCompleteCallback: (x?: Error) => void; + _newDocumentNavigationCompleteCallback: (x?: Error) => void = noop; + _newDocumentNavigationPromise: Promise = new Promise( + (fulfill) => { + this._newDocumentNavigationCompleteCallback = fulfill; + } + ); - _terminationPromise: Promise; - _terminationCallback: (x?: Error) => void; + _terminationCallback: (x?: Error) => void = noop; + _terminationPromise: Promise = new Promise((fulfill) => { + this._terminationCallback = fulfill; + }); - _timeoutPromise: Promise; + _timeoutPromise: Promise; _maximumTimer?: NodeJS.Timeout; _hasSameDocumentNavigation?: boolean; @@ -95,14 +107,13 @@ export class LifecycleWatcher { this._expectedLifecycle = waitUntil.map((value) => { const protocolEvent = puppeteerToProtocolLifecycle.get(value); assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value); - return protocolEvent; + return protocolEvent as ProtocolLifeCycleEvent; }); this._frameManager = frameManager; this._frame = frame; this._initialLoaderId = frame._loaderId; this._timeout = timeout; - this._navigationRequest = null; this._eventListeners = [ helper.addEventListener( frameManager._client, @@ -139,24 +150,7 @@ export class LifecycleWatcher { ), ]; - this._sameDocumentNavigationPromise = new Promise( - (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._terminationPromise = new Promise((fulfill) => { - this._terminationCallback = fulfill; - }); this._checkLifecycleComplete(); } @@ -186,11 +180,11 @@ export class LifecycleWatcher { this._terminationCallback.call(null, error); } - sameDocumentNavigationPromise(): Promise { + sameDocumentNavigationPromise(): Promise { return this._sameDocumentNavigationPromise; } - newDocumentNavigationPromise(): Promise { + newDocumentNavigationPromise(): Promise { return this._newDocumentNavigationPromise; } @@ -198,11 +192,11 @@ export class LifecycleWatcher { return this._lifecyclePromise; } - timeoutOrTerminationPromise(): Promise { + timeoutOrTerminationPromise(): Promise { return Promise.race([this._timeoutPromise, this._terminationPromise]); } - _createTimeoutPromise(): Promise { + _createTimeoutPromise(): Promise { if (!this._timeout) return new Promise(() => {}); const errorMessage = 'Navigation timeout of ' + this._timeout + ' ms exceeded'; @@ -268,6 +262,6 @@ export class LifecycleWatcher { dispose(): void { helper.removeEventListeners(this._eventListeners); - clearTimeout(this._maximumTimer); + this._maximumTimer !== undefined && clearTimeout(this._maximumTimer); } }