Skip to content

Commit

Permalink
refactor: move waitWithNetworkIdle (#11321)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Nov 8, 2023
1 parent b73dec0 commit 1927fb5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 66 deletions.
82 changes: 25 additions & 57 deletions packages/puppeteer-core/src/bidi/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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))
);
Expand All @@ -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))
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
})
);
}
}
53 changes: 44 additions & 9 deletions packages/puppeteer-core/src/bidi/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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))
);
Expand Down Expand Up @@ -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> {
const {sessionId} = await this.mainFrame()
.context()
Expand Down

0 comments on commit 1927fb5

Please sign in to comment.