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,
raceWith,
switchMap,
forkJoin,
first,
} from '../../third_party/rxjs/rxjs.js';
import type {CDPSession} from '../api/CDPSession.js';
import {
@ -179,25 +181,19 @@ export class BidiFrame extends Frame {
): Promise<void> {
const {
waitUntil = 'load',
timeout = this.#timeoutSettings.navigationTimeout(),
timeout: ms = this.#timeoutSettings.navigationTimeout(),
} = options;
const waitUntilEvent = lifeCycleToSubscribedEvent.get(
getWaitUntilSingle(waitUntil)
) as string;
await Promise.all([
setPageContent(this, html),
waitWithTimeout(
new Promise<void>(resolve => {
this.#context.once(waitUntilEvent, () => {
resolve();
});
}),
waitUntilEvent,
timeout
),
]);
await firstValueFrom(
forkJoin([
fromEvent(this.#context, waitUntilEvent).pipe(first()),
from(setPageContent(this, html)),
]).pipe(raceWith(timeout(ms)))
);
}
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 Protocol from 'devtools-protocol';
import {firstValueFrom, from, raceWith} from 'rxjs';
import type {CDPSession} from '../api/CDPSession.js';
import type {WaitForOptions} from '../api/Frame.js';
@ -50,6 +51,7 @@ import type {Awaitable} from '../common/types.js';
import {
debugError,
evaluationString,
timeout,
validateDialogType,
waitForHTTP,
waitWithTimeout,
@ -599,25 +601,25 @@ export class BidiPage extends Page {
pageRanges: ranges,
scale,
preferCSSPageSize,
timeout,
timeout: ms,
} = this._getPDFOptions(options, 'cm');
const pageRanges = ranges ? ranges.split(', ') : [];
const {result} = await waitWithTimeout(
this.#connection.send('browsingContext.print', {
context: this.mainFrame()._id,
background,
margin,
orientation: landscape ? 'landscape' : 'portrait',
page: {
width,
height,
},
pageRanges,
scale,
shrinkToFit: !preferCSSPageSize,
}),
'browsingContext.print',
timeout
const {result} = await firstValueFrom(
from(
this.#connection.send('browsingContext.print', {
context: this.mainFrame()._id,
background,
margin,
orientation: landscape ? 'landscape' : 'portrait',
page: {
width,
height,
},
pageRanges,
scale,
shrinkToFit: !preferCSSPageSize,
})
).pipe(raceWith(timeout(ms)))
);
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 {firstValueFrom, from, raceWith} from '../../third_party/rxjs/rxjs.js';
import type {Browser} from '../api/Browser.js';
import type {BrowserContext} from '../api/BrowserContext.js';
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
@ -53,10 +54,10 @@ import {
getReadableAsBuffer,
getReadableFromProtocolStream,
pageBindingInitString,
timeout,
validateDialogType,
valueFromRemoteObject,
waitForHTTP,
waitWithTimeout,
} from '../common/util.js';
import type {Viewport} from '../common/Viewport.js';
import {assert} from '../util/assert.js';
@ -1138,7 +1139,7 @@ export class CdpPage extends Page {
pageRanges,
preferCSSPageSize,
omitBackground,
timeout,
timeout: ms,
} = this._getPDFOptions(options);
if (omitBackground) {
@ -1166,10 +1167,8 @@ export class CdpPage extends Page {
}
);
const result = await waitWithTimeout(
printCommandPromise,
'Page.printToPDF',
timeout
const result = await firstValueFrom(
from(printCommandPromise).pipe(raceWith(timeout(ms)))
);
if (omitBackground) {

View File

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