diff --git a/packages/puppeteer-core/src/common/Frame.ts b/packages/puppeteer-core/src/common/Frame.ts index 7462201c..bbead34c 100644 --- a/packages/puppeteer-core/src/common/Frame.ts +++ b/packages/puppeteer-core/src/common/Frame.ts @@ -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(), ]); diff --git a/packages/puppeteer-core/src/common/IsolatedWorld.ts b/packages/puppeteer-core/src/common/IsolatedWorld.ts index 43bd4cd8..96a2e0f8 100644 --- a/packages/puppeteer-core/src/common/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/common/IsolatedWorld.ts @@ -299,7 +299,7 @@ export class IsolatedWorld implements Realm { timeout ); const error = await Deferred.race([ - watcher.timeoutOrTerminationPromise(), + watcher.terminationPromise(), watcher.lifecyclePromise(), ]); watcher.dispose(); diff --git a/packages/puppeteer-core/src/common/LifecycleWatcher.ts b/packages/puppeteer-core/src/common/LifecycleWatcher.ts index f2dfb0d1..8a905029 100644 --- a/packages/puppeteer-core/src/common/LifecycleWatcher.ts +++ b/packages/puppeteer-core/src/common/LifecycleWatcher.ts @@ -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; #sameDocumentNavigationDeferred = Deferred.create(); #lifecycleDeferred = Deferred.create(); #newDocumentNavigationDeferred = Deferred.create(); - #terminationDeferred = Deferred.create(); - #timeoutPromise: Promise; - - #maximumTimer?: NodeJS.Timeout; #hasSameDocumentNavigation?: boolean; #swapped?: boolean; @@ -156,7 +151,11 @@ export class LifecycleWatcher { ), ]; - this.#timeoutPromise = this.#createTimeoutPromise(); + this.#terminationDeferred = Deferred.create({ + 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 { - return Deferred.race([this.#timeoutPromise, this.#terminationDeferred]); - } - - async #createTimeoutPromise(): Promise { - 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 { + 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')); } } diff --git a/packages/puppeteer-core/src/common/util.ts b/packages/puppeteer-core/src/common/util.ts index 5ac0083a..27c7a013 100644 --- a/packages/puppeteer-core/src/common/util.ts +++ b/packages/puppeteer-core/src/common/util.ts @@ -513,9 +513,7 @@ export async function waitWithTimeout( timeout, }); - return await Deferred.race([promise, deferred]).finally(() => { - deferred.reject(new Error('Cleared')); - }); + return await Deferred.race([promise, deferred]); } /**