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