chore: use RxJS instead of waitWithTimeout (#11160)

This commit is contained in:
Nikolay Vitkov 2023-10-16 14:37:52 +02:00 committed by GitHub
parent 511614fe44
commit 5ce3abe675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 46 deletions

View File

@ -24,6 +24,8 @@ import {
merge, merge,
raceWith, raceWith,
switchMap, switchMap,
forkJoin,
first,
} from '../../third_party/rxjs/rxjs.js'; } from '../../third_party/rxjs/rxjs.js';
import type {CDPSession} from '../api/CDPSession.js'; import type {CDPSession} from '../api/CDPSession.js';
import { import {
@ -179,25 +181,19 @@ export class BidiFrame extends Frame {
): Promise<void> { ): Promise<void> {
const { const {
waitUntil = 'load', waitUntil = 'load',
timeout = this.#timeoutSettings.navigationTimeout(), timeout: ms = this.#timeoutSettings.navigationTimeout(),
} = options; } = options;
const waitUntilEvent = lifeCycleToSubscribedEvent.get( const waitUntilEvent = lifeCycleToSubscribedEvent.get(
getWaitUntilSingle(waitUntil) getWaitUntilSingle(waitUntil)
) as string; ) as string;
await Promise.all([ await firstValueFrom(
setPageContent(this, html), forkJoin([
waitWithTimeout( fromEvent(this.#context, waitUntilEvent).pipe(first()),
new Promise<void>(resolve => { from(setPageContent(this, html)),
this.#context.once(waitUntilEvent, () => { ]).pipe(raceWith(timeout(ms)))
resolve(); );
});
}),
waitUntilEvent,
timeout
),
]);
} }
context(): BrowsingContext { context(): BrowsingContext {

View File

@ -18,6 +18,7 @@ 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 'rxjs';
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';
@ -50,6 +51,7 @@ import type {Awaitable} from '../common/types.js';
import { import {
debugError, debugError,
evaluationString, evaluationString,
timeout,
validateDialogType, validateDialogType,
waitForHTTP, waitForHTTP,
waitWithTimeout, waitWithTimeout,
@ -599,25 +601,25 @@ export class BidiPage extends Page {
pageRanges: ranges, pageRanges: ranges,
scale, scale,
preferCSSPageSize, preferCSSPageSize,
timeout, timeout: ms,
} = this._getPDFOptions(options, 'cm'); } = this._getPDFOptions(options, 'cm');
const pageRanges = ranges ? ranges.split(', ') : []; const pageRanges = ranges ? ranges.split(', ') : [];
const {result} = await waitWithTimeout( const {result} = await firstValueFrom(
this.#connection.send('browsingContext.print', { from(
context: this.mainFrame()._id, this.#connection.send('browsingContext.print', {
background, context: this.mainFrame()._id,
margin, background,
orientation: landscape ? 'landscape' : 'portrait', margin,
page: { orientation: landscape ? 'landscape' : 'portrait',
width, page: {
height, width,
}, height,
pageRanges, },
scale, pageRanges,
shrinkToFit: !preferCSSPageSize, scale,
}), shrinkToFit: !preferCSSPageSize,
'browsingContext.print', })
timeout ).pipe(raceWith(timeout(ms)))
); );
const buffer = Buffer.from(result.data, 'base64'); const buffer = Buffer.from(result.data, 'base64');

View File

@ -18,6 +18,7 @@ import type {Readable} from 'stream';
import type {Protocol} from 'devtools-protocol'; import type {Protocol} from 'devtools-protocol';
import {firstValueFrom, from, raceWith} from '../../third_party/rxjs/rxjs.js';
import type {Browser} from '../api/Browser.js'; import type {Browser} from '../api/Browser.js';
import type {BrowserContext} from '../api/BrowserContext.js'; import type {BrowserContext} from '../api/BrowserContext.js';
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js'; import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
@ -53,10 +54,10 @@ import {
getReadableAsBuffer, getReadableAsBuffer,
getReadableFromProtocolStream, getReadableFromProtocolStream,
pageBindingInitString, pageBindingInitString,
timeout,
validateDialogType, validateDialogType,
valueFromRemoteObject, valueFromRemoteObject,
waitForHTTP, waitForHTTP,
waitWithTimeout,
} from '../common/util.js'; } from '../common/util.js';
import type {Viewport} from '../common/Viewport.js'; import type {Viewport} from '../common/Viewport.js';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
@ -1138,7 +1139,7 @@ export class CdpPage extends Page {
pageRanges, pageRanges,
preferCSSPageSize, preferCSSPageSize,
omitBackground, omitBackground,
timeout, timeout: ms,
} = this._getPDFOptions(options); } = this._getPDFOptions(options);
if (omitBackground) { if (omitBackground) {
@ -1166,10 +1167,8 @@ export class CdpPage extends Page {
} }
); );
const result = await waitWithTimeout( const result = await firstValueFrom(
printCommandPromise, from(printCommandPromise).pipe(raceWith(timeout(ms)))
'Page.printToPDF',
timeout
); );
if (omitBackground) { if (omitBackground) {

View File

@ -47,6 +47,7 @@ export {
tap, tap,
throwIfEmpty, throwIfEmpty,
timer, timer,
forkJoin,
} from 'rxjs'; } from 'rxjs';
import {filter, from, map, mergeMap, type Observable} from 'rxjs'; import {filter, from, map, mergeMap, type Observable} from 'rxjs';

View File

@ -18,7 +18,7 @@ import expect from 'expect';
import {TimeoutError} from 'puppeteer-core'; import {TimeoutError} from 'puppeteer-core';
import { import {
Locator, Locator,
LocatorEmittedEvents, LocatorEvent,
} from 'puppeteer-core/internal/api/locators/locators.js'; } from 'puppeteer-core/internal/api/locators/locators.js';
import sinon from 'sinon'; import sinon from 'sinon';
@ -38,7 +38,7 @@ describe('Locator', function () {
await page await page
.mainFrame() .mainFrame()
.locator('button') .locator('button')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
willClick = true; willClick = true;
}) })
.click(); .click();
@ -65,7 +65,7 @@ describe('Locator', function () {
.setVisibility(null) .setVisibility(null)
.setWaitForEnabled(false) .setWaitForEnabled(false)
.setWaitForStableBoundingBox(false) .setWaitForStableBoundingBox(false)
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
willClick = true; willClick = true;
}) })
.click(); .click();
@ -88,7 +88,7 @@ describe('Locator', function () {
let willClick = false; let willClick = false;
await page await page
.locator('button') .locator('button')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
willClick = true; willClick = true;
}) })
.click(); .click();
@ -110,7 +110,7 @@ describe('Locator', function () {
let clicked = false; let clicked = false;
await page await page
.locator('::-p-text(test), ::-p-xpath(/button)') .locator('::-p-text(test), ::-p-xpath(/button)')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
clicked = true; clicked = true;
}) })
.click(); .click();
@ -304,7 +304,7 @@ describe('Locator', function () {
let willClick = false; let willClick = false;
await frame await frame
.locator('button') .locator('button')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
willClick = true; willClick = true;
}) })
.click(); .click();
@ -328,7 +328,7 @@ describe('Locator', function () {
let hovered = false; let hovered = false;
await page await page
.locator('button') .locator('button')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
hovered = true; hovered = true;
}) })
.hover(); .hover();
@ -354,7 +354,7 @@ describe('Locator', function () {
let scrolled = false; let scrolled = false;
await page await page
.locator('div') .locator('div')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
scrolled = true; scrolled = true;
}) })
.scroll({ .scroll({
@ -380,7 +380,7 @@ describe('Locator', function () {
let filled = false; let filled = false;
await page await page
.locator('textarea') .locator('textarea')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
filled = true; filled = true;
}) })
.fill('test'); .fill('test');
@ -404,7 +404,7 @@ describe('Locator', function () {
let filled = false; let filled = false;
await page await page
.locator('select') .locator('select')
.on(LocatorEmittedEvents.Action, () => { .on(LocatorEvent.Action, () => {
filled = true; filled = true;
}) })
.fill('value2'); .fill('value2');