Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix strict-mode TS in LifecycleWatcher #8387

Merged
merged 1 commit into from May 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 28 additions & 34 deletions src/common/LifecycleWatcher.ts
Expand Up @@ -54,6 +54,8 @@ const puppeteerToProtocolLifecycle = new Map<
['networkidle2', 'networkAlmostIdle'],
]);

const noop = (): void => {};

/**
* @internal
*/
Expand All @@ -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<Error | null>;
_sameDocumentNavigationCompleteCallback: (x?: Error) => void;
_sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
_sameDocumentNavigationPromise = new Promise<Error | undefined>((fulfill) => {
this._sameDocumentNavigationCompleteCallback = fulfill;
});

_lifecyclePromise: Promise<void>;
_lifecycleCallback: () => void;
_lifecycleCallback: () => void = noop;
_lifecyclePromise: Promise<void> = new Promise((fulfill) => {
this._lifecycleCallback = fulfill;
});

_newDocumentNavigationPromise: Promise<Error | null>;
_newDocumentNavigationCompleteCallback: (x?: Error) => void;
_newDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
_newDocumentNavigationPromise: Promise<Error | undefined> = new Promise(
(fulfill) => {
this._newDocumentNavigationCompleteCallback = fulfill;
}
);

_terminationPromise: Promise<Error | null>;
_terminationCallback: (x?: Error) => void;
_terminationCallback: (x?: Error) => void = noop;
_terminationPromise: Promise<Error | undefined> = new Promise((fulfill) => {
this._terminationCallback = fulfill;
});

_timeoutPromise: Promise<TimeoutError | null>;
_timeoutPromise: Promise<TimeoutError | undefined>;

_maximumTimer?: NodeJS.Timeout;
_hasSameDocumentNavigation?: boolean;
Expand All @@ -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,
Expand Down Expand Up @@ -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._terminationPromise = new Promise((fulfill) => {
this._terminationCallback = fulfill;
});
this._checkLifecycleComplete();
}

Expand Down Expand Up @@ -186,23 +180,23 @@ export class LifecycleWatcher {
this._terminationCallback.call(null, error);
}

sameDocumentNavigationPromise(): Promise<Error | null> {
sameDocumentNavigationPromise(): Promise<Error | undefined> {
return this._sameDocumentNavigationPromise;
}

newDocumentNavigationPromise(): Promise<Error | null> {
newDocumentNavigationPromise(): Promise<Error | undefined> {
return this._newDocumentNavigationPromise;
}

lifecyclePromise(): Promise<void> {
return this._lifecyclePromise;
}

timeoutOrTerminationPromise(): Promise<Error | TimeoutError | null> {
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
return Promise.race([this._timeoutPromise, this._terminationPromise]);
}

_createTimeoutPromise(): Promise<TimeoutError | null> {
_createTimeoutPromise(): Promise<TimeoutError | undefined> {
if (!this._timeout) return new Promise(() => {});
const errorMessage =
'Navigation timeout of ' + this._timeout + ' ms exceeded';
Expand Down Expand Up @@ -268,6 +262,6 @@ export class LifecycleWatcher {

dispose(): void {
helper.removeEventListeners(this._eventListeners);
clearTimeout(this._maximumTimer);
this._maximumTimer !== undefined && clearTimeout(this._maximumTimer);
}
}