refactor: move waitWithNetworkIdle (#11321)

This commit is contained in:
Nikolay Vitkov 2023-11-08 14:31:34 +01:00 committed by GitHub
parent b73dec0134
commit 1927fb561e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 66 deletions

View File

@ -16,7 +16,6 @@
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js'; import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import type {ObservableInput} from '../../third_party/rxjs/rxjs.js';
import { import {
type Observable, type Observable,
from, from,
@ -37,19 +36,13 @@ import {
} from '../api/Frame.js'; } from '../api/Frame.js';
import type {TimeoutSettings} from '../common/TimeoutSettings.js'; import type {TimeoutSettings} from '../common/TimeoutSettings.js';
import type {Awaitable} from '../common/types.js'; import type {Awaitable} from '../common/types.js';
import { import {UTILITY_WORLD_NAME, setPageContent, timeout} from '../common/util.js';
NETWORK_IDLE_TIME,
UTILITY_WORLD_NAME,
setPageContent,
timeout,
} from '../common/util.js';
import {Deferred} from '../util/Deferred.js'; import {Deferred} from '../util/Deferred.js';
import {disposeSymbol} from '../util/disposable.js'; import {disposeSymbol} from '../util/disposable.js';
import type {BrowsingContext} from './BrowsingContext.js'; import type {BrowsingContext} from './BrowsingContext.js';
import {ExposeableFunction} from './ExposedFunction.js'; import {ExposeableFunction} from './ExposedFunction.js';
import type {BidiHTTPResponse} from './HTTPResponse.js'; import type {BidiHTTPResponse} from './HTTPResponse.js';
import type {BiDiNetworkIdle} from './lifecycle.js';
import { import {
getBiDiLifecycleEvent, getBiDiLifecycleEvent,
getBiDiReadinessState, getBiDiReadinessState,
@ -141,14 +134,15 @@ export class BidiFrame extends Frame {
const [readiness, networkIdle] = getBiDiReadinessState(waitUntil); const [readiness, networkIdle] = getBiDiReadinessState(waitUntil);
const response = await firstValueFrom( const response = await firstValueFrom(
this._waitWithNetworkIdle( this.#page
this.#context.connection.send('browsingContext.navigate', { ._waitWithNetworkIdle(
context: this.#context.id, this.#context.connection.send('browsingContext.navigate', {
url, context: this.#context.id,
wait: readiness, url,
}), wait: readiness,
networkIdle }),
) networkIdle
)
.pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow()))) .pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow())))
.pipe(rewriteNavigationError(url, ms)) .pipe(rewriteNavigationError(url, ms))
); );
@ -169,17 +163,18 @@ export class BidiFrame extends Frame {
const [waitEvent, networkIdle] = getBiDiLifecycleEvent(waitUntil); const [waitEvent, networkIdle] = getBiDiLifecycleEvent(waitUntil);
await firstValueFrom( await firstValueFrom(
this._waitWithNetworkIdle( this.#page
forkJoin([ ._waitWithNetworkIdle(
fromEvent(this.#context, waitEvent).pipe(first()), forkJoin([
from(setPageContent(this, html)), fromEvent(this.#context, waitEvent).pipe(first()),
]).pipe( from(setPageContent(this, html)),
map(() => { ]).pipe(
return null; map(() => {
}) return null;
), })
networkIdle ),
) networkIdle
)
.pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow()))) .pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow())))
.pipe(rewriteNavigationError('setContent', ms)) .pipe(rewriteNavigationError('setContent', ms))
); );
@ -224,9 +219,9 @@ export class BidiFrame extends Frame {
); );
const response = await firstValueFrom( const response = await firstValueFrom(
this._waitWithNetworkIdle(navigatedObservable, networkIdle).pipe( this.#page
raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow())) ._waitWithNetworkIdle(navigatedObservable, networkIdle)
) .pipe(raceWith(timeout(ms), from(this.#abortDeferred.valueOrThrow())))
); );
return this.#page.getNavigationResponse(response?.result.navigation); return this.#page.getNavigationResponse(response?.result.navigation);
@ -266,31 +261,4 @@ export class BidiFrame extends Frame {
throw error; 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;
})
);
}
} }

View File

@ -19,7 +19,15 @@ import type {Readable} from 'stream';
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js'; import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import type Protocol from 'devtools-protocol'; 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 {CDPSession} from '../api/CDPSession.js';
import type {WaitForOptions} from '../api/Frame.js'; import type {WaitForOptions} from '../api/Frame.js';
import { import {
@ -74,6 +82,7 @@ import type {BidiHTTPRequest} from './HTTPRequest.js';
import type {BidiHTTPResponse} from './HTTPResponse.js'; import type {BidiHTTPResponse} from './HTTPResponse.js';
import {BidiKeyboard, BidiMouse, BidiTouchscreen} from './Input.js'; import {BidiKeyboard, BidiMouse, BidiTouchscreen} from './Input.js';
import type {BidiJSHandle} from './JSHandle.js'; import type {BidiJSHandle} from './JSHandle.js';
import type {BiDiNetworkIdle} from './lifecycle.js';
import {getBiDiReadinessState, rewriteNavigationError} from './lifecycle.js'; import {getBiDiReadinessState, rewriteNavigationError} from './lifecycle.js';
import {BidiNetworkManager} from './NetworkManager.js'; import {BidiNetworkManager} from './NetworkManager.js';
import {createBidiHandle} from './Realm.js'; import {createBidiHandle} from './Realm.js';
@ -491,14 +500,13 @@ export class BidiPage extends Page {
const [readiness, networkIdle] = getBiDiReadinessState(waitUntil); const [readiness, networkIdle] = getBiDiReadinessState(waitUntil);
const response = await firstValueFrom( const response = await firstValueFrom(
this.mainFrame() this._waitWithNetworkIdle(
._waitWithNetworkIdle( this.#connection.send('browsingContext.reload', {
this.#connection.send('browsingContext.reload', { context: this.mainFrame()._id,
context: this.mainFrame()._id, wait: readiness,
wait: readiness, }),
}), networkIdle
networkIdle )
)
.pipe(raceWith(timeout(ms), from(this.#closedDeferred.valueOrThrow()))) .pipe(raceWith(timeout(ms), from(this.#closedDeferred.valueOrThrow())))
.pipe(rewriteNavigationError(this.url(), ms)) .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<CDPSession> { override async createCDPSession(): Promise<CDPSession> {
const {sessionId} = await this.mainFrame() const {sessionId} = await this.mainFrame()
.context() .context()