refactor: simplified the LifecycleWatcher (#10487)
This commit is contained in:
parent
58e9c64f63
commit
089e90e3f8
@ -126,11 +126,11 @@ export class Frame extends BaseFrame {
|
||||
referrerPolicy as Protocol.Page.ReferrerPolicy,
|
||||
this._id
|
||||
),
|
||||
watcher.timeoutOrTerminationPromise(),
|
||||
watcher.terminationPromise(),
|
||||
]);
|
||||
if (!error) {
|
||||
error = await Deferred.race([
|
||||
watcher.timeoutOrTerminationPromise(),
|
||||
watcher.terminationPromise(),
|
||||
ensureNewDocumentNavigation
|
||||
? watcher.newDocumentNavigationPromise()
|
||||
: watcher.sameDocumentNavigationPromise(),
|
||||
@ -193,7 +193,7 @@ export class Frame extends BaseFrame {
|
||||
timeout
|
||||
);
|
||||
const error = await Deferred.race([
|
||||
watcher.timeoutOrTerminationPromise(),
|
||||
watcher.terminationPromise(),
|
||||
watcher.sameDocumentNavigationPromise(),
|
||||
watcher.newDocumentNavigationPromise(),
|
||||
]);
|
||||
|
@ -299,7 +299,7 @@ export class IsolatedWorld implements Realm {
|
||||
timeout
|
||||
);
|
||||
const error = await Deferred.race<void | Error | undefined>([
|
||||
watcher.timeoutOrTerminationPromise(),
|
||||
watcher.terminationPromise(),
|
||||
watcher.lifecyclePromise(),
|
||||
]);
|
||||
watcher.dispose();
|
||||
|
@ -57,8 +57,6 @@ const puppeteerToProtocolLifecycle = new Map<
|
||||
['networkidle2', 'networkAlmostIdle'],
|
||||
]);
|
||||
|
||||
const noop = (): void => {};
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@ -71,14 +69,11 @@ export class LifecycleWatcher {
|
||||
#eventListeners: PuppeteerEventListener[];
|
||||
#initialLoaderId: string;
|
||||
|
||||
#terminationDeferred: Deferred<Error>;
|
||||
#sameDocumentNavigationDeferred = Deferred.create<undefined>();
|
||||
#lifecycleDeferred = Deferred.create<void>();
|
||||
#newDocumentNavigationDeferred = Deferred.create<undefined>();
|
||||
#terminationDeferred = Deferred.create<Error>();
|
||||
|
||||
#timeoutPromise: Promise<TimeoutError | undefined>;
|
||||
|
||||
#maximumTimer?: NodeJS.Timeout;
|
||||
#hasSameDocumentNavigation?: boolean;
|
||||
#swapped?: boolean;
|
||||
|
||||
@ -156,7 +151,11 @@ export class LifecycleWatcher {
|
||||
),
|
||||
];
|
||||
|
||||
this.#timeoutPromise = this.#createTimeoutPromise();
|
||||
this.#terminationDeferred = Deferred.create<Error>({
|
||||
timeout: this.#timeout,
|
||||
message: `Navigation timeout of ${this.#timeout} ms exceeded`,
|
||||
});
|
||||
|
||||
this.#checkLifecycleComplete();
|
||||
}
|
||||
|
||||
@ -221,20 +220,8 @@ export class LifecycleWatcher {
|
||||
return this.#lifecycleDeferred.valueOrThrow();
|
||||
}
|
||||
|
||||
timeoutOrTerminationPromise(): Promise<Error | TimeoutError | undefined> {
|
||||
return Deferred.race([this.#timeoutPromise, this.#terminationDeferred]);
|
||||
}
|
||||
|
||||
async #createTimeoutPromise(): Promise<TimeoutError | undefined> {
|
||||
if (!this.#timeout) {
|
||||
return new Promise(noop);
|
||||
}
|
||||
const errorMessage =
|
||||
'Navigation timeout of ' + this.#timeout + ' ms exceeded';
|
||||
await new Promise(fulfill => {
|
||||
return (this.#maximumTimer = setTimeout(fulfill, this.#timeout));
|
||||
});
|
||||
return new TimeoutError(errorMessage);
|
||||
terminationPromise(): Promise<Error | TimeoutError | undefined> {
|
||||
return this.#terminationDeferred.valueOrThrow();
|
||||
}
|
||||
|
||||
#navigatedWithinDocument(frame: Frame): void {
|
||||
@ -296,6 +283,6 @@ export class LifecycleWatcher {
|
||||
|
||||
dispose(): void {
|
||||
removeEventListeners(this.#eventListeners);
|
||||
clearTimeout(this.#maximumTimer);
|
||||
this.#terminationDeferred.resolve(new Error('LifecycleWatcher disposed'));
|
||||
}
|
||||
}
|
||||
|
@ -513,9 +513,7 @@ export async function waitWithTimeout<T>(
|
||||
timeout,
|
||||
});
|
||||
|
||||
return await Deferred.race([promise, deferred]).finally(() => {
|
||||
deferred.reject(new Error('Cleared'));
|
||||
});
|
||||
return await Deferred.race([promise, deferred]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user