From 1927fb561e74da15347a9c75280fedd356408424 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:31:34 +0100 Subject: [PATCH] refactor: move waitWithNetworkIdle (#11321) --- packages/puppeteer-core/src/bidi/Frame.ts | 82 +++++++---------------- packages/puppeteer-core/src/bidi/Page.ts | 53 ++++++++++++--- 2 files changed, 69 insertions(+), 66 deletions(-) diff --git a/packages/puppeteer-core/src/bidi/Frame.ts b/packages/puppeteer-core/src/bidi/Frame.ts index 72e0ded0..93199fde 100644 --- a/packages/puppeteer-core/src/bidi/Frame.ts +++ b/packages/puppeteer-core/src/bidi/Frame.ts @@ -16,7 +16,6 @@ import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js'; -import type {ObservableInput} from '../../third_party/rxjs/rxjs.js'; import { type Observable, from, @@ -37,19 +36,13 @@ import { } from '../api/Frame.js'; import type {TimeoutSettings} from '../common/TimeoutSettings.js'; import type {Awaitable} from '../common/types.js'; -import { - NETWORK_IDLE_TIME, - UTILITY_WORLD_NAME, - setPageContent, - timeout, -} from '../common/util.js'; +import {UTILITY_WORLD_NAME, setPageContent, timeout} from '../common/util.js'; import {Deferred} from '../util/Deferred.js'; import {disposeSymbol} from '../util/disposable.js'; import type {BrowsingContext} from './BrowsingContext.js'; import {ExposeableFunction} from './ExposedFunction.js'; import type {BidiHTTPResponse} from './HTTPResponse.js'; -import type {BiDiNetworkIdle} from './lifecycle.js'; import { getBiDiLifecycleEvent, getBiDiReadinessState, @@ -141,14 +134,15 @@ export class BidiFrame extends Frame { const [readiness, networkIdle] = getBiDiReadinessState(waitUntil); const response = await firstValueFrom( - this._waitWithNetworkIdle( - this.#context.connection.send('browsingContext.navigate', { - context: this.#context.id, - url, - wait: readiness, - }), - networkIdle - ) + this.#page + ._waitWithNetworkIdle( + this.#context.connection.send('browsingContext.navigate', { + context: this.#context.id, + url, + wait: readiness, + }), + networkIdle + ) .pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow()))) .pipe(rewriteNavigationError(url, ms)) ); @@ -169,17 +163,18 @@ export class BidiFrame extends Frame { const [waitEvent, networkIdle] = getBiDiLifecycleEvent(waitUntil); await firstValueFrom( - this._waitWithNetworkIdle( - forkJoin([ - fromEvent(this.#context, waitEvent).pipe(first()), - from(setPageContent(this, html)), - ]).pipe( - map(() => { - return null; - }) - ), - networkIdle - ) + this.#page + ._waitWithNetworkIdle( + forkJoin([ + fromEvent(this.#context, waitEvent).pipe(first()), + from(setPageContent(this, html)), + ]).pipe( + map(() => { + return null; + }) + ), + networkIdle + ) .pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow()))) .pipe(rewriteNavigationError('setContent', ms)) ); @@ -224,9 +219,9 @@ export class BidiFrame extends Frame { ); const response = await firstValueFrom( - this._waitWithNetworkIdle(navigatedObservable, networkIdle).pipe( - raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow())) - ) + this.#page + ._waitWithNetworkIdle(navigatedObservable, networkIdle) + .pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow()))) ); return this.#page.getNavigationResponse(response?.result.navigation); @@ -266,31 +261,4 @@ export class BidiFrame extends Frame { throw error; } } - - /** @internal */ - _waitWithNetworkIdle( - observableInput: ObservableInput<{ - result: Bidi.BrowsingContext.NavigateResult; - } | null>, - networkIdle: BiDiNetworkIdle - ): Observable<{ - result: Bidi.BrowsingContext.NavigateResult; - } | null> { - const delay = networkIdle - ? this.#page._waitForNetworkIdle( - this.#page._networkManager, - NETWORK_IDLE_TIME, - networkIdle === 'networkidle0' ? 0 : 2 - ) - : from(Promise.resolve()); - - return forkJoin([ - from(observableInput).pipe(first()), - delay.pipe(first()), - ]).pipe( - map(([response]) => { - return response; - }) - ); - } } diff --git a/packages/puppeteer-core/src/bidi/Page.ts b/packages/puppeteer-core/src/bidi/Page.ts index 76ddd4e5..7ba226ec 100644 --- a/packages/puppeteer-core/src/bidi/Page.ts +++ b/packages/puppeteer-core/src/bidi/Page.ts @@ -19,7 +19,15 @@ import type {Readable} from 'stream'; import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js'; import type Protocol from 'devtools-protocol'; -import {firstValueFrom, from, raceWith} from '../../third_party/rxjs/rxjs.js'; +import type {Observable, ObservableInput} from '../../third_party/rxjs/rxjs.js'; +import { + first, + firstValueFrom, + forkJoin, + from, + map, + raceWith, +} from '../../third_party/rxjs/rxjs.js'; import type {CDPSession} from '../api/CDPSession.js'; import type {WaitForOptions} from '../api/Frame.js'; import { @@ -74,6 +82,7 @@ import type {BidiHTTPRequest} from './HTTPRequest.js'; import type {BidiHTTPResponse} from './HTTPResponse.js'; import {BidiKeyboard, BidiMouse, BidiTouchscreen} from './Input.js'; import type {BidiJSHandle} from './JSHandle.js'; +import type {BiDiNetworkIdle} from './lifecycle.js'; import {getBiDiReadinessState, rewriteNavigationError} from './lifecycle.js'; import {BidiNetworkManager} from './NetworkManager.js'; import {createBidiHandle} from './Realm.js'; @@ -491,14 +500,13 @@ export class BidiPage extends Page { const [readiness, networkIdle] = getBiDiReadinessState(waitUntil); const response = await firstValueFrom( - this.mainFrame() - ._waitWithNetworkIdle( - this.#connection.send('browsingContext.reload', { - context: this.mainFrame()._id, - wait: readiness, - }), - networkIdle - ) + this._waitWithNetworkIdle( + this.#connection.send('browsingContext.reload', { + context: this.mainFrame()._id, + wait: readiness, + }), + networkIdle + ) .pipe(raceWith(timeout(ms), from(this.#closedDeferred.valueOrThrow()))) .pipe(rewriteNavigationError(this.url(), ms)) ); @@ -720,6 +728,33 @@ export class BidiPage extends Page { ); } + /** @internal */ + _waitWithNetworkIdle( + observableInput: ObservableInput<{ + result: Bidi.BrowsingContext.NavigateResult; + } | null>, + networkIdle: BiDiNetworkIdle + ): Observable<{ + result: Bidi.BrowsingContext.NavigateResult; + } | null> { + const delay = networkIdle + ? this._waitForNetworkIdle( + this._networkManager, + NETWORK_IDLE_TIME, + networkIdle === 'networkidle0' ? 0 : 2 + ) + : from(Promise.resolve()); + + return forkJoin([ + from(observableInput).pipe(first()), + delay.pipe(first()), + ]).pipe( + map(([response]) => { + return response; + }) + ); + } + override async createCDPSession(): Promise { const {sessionId} = await this.mainFrame() .context()