refactor: use generic implementation for waitForResponse (#11752)

This commit is contained in:
jrandolf 2024-01-25 14:22:48 +01:00 committed by GitHub
parent 3a4a23faa8
commit 3698fa8aec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 51 additions and 49 deletions

View File

@ -170,6 +170,7 @@ sidebar_label: API
| [ActionResult](./puppeteer.actionresult.md) | |
| [Awaitable](./puppeteer.awaitable.md) | |
| [AwaitableIterable](./puppeteer.awaitableiterable.md) | |
| [AwaitablePredicate](./puppeteer.awaitablepredicate.md) | |
| [AwaitedLocator](./puppeteer.awaitedlocator.md) | |
| [CDPEvents](./puppeteer.cdpevents.md) | |
| [ChromeReleaseChannel](./puppeteer.chromereleasechannel.md) | |

View File

@ -0,0 +1,13 @@
---
sidebar_label: AwaitablePredicate
---
# AwaitablePredicate type
#### Signature:
```typescript
export type AwaitablePredicate<T> = (value: T) => Awaitable<boolean>;
```
**References:** [Awaitable](./puppeteer.awaitable.md)

View File

@ -8,23 +8,19 @@ sidebar_label: Page.waitForResponse
```typescript
class Page {
abstract waitForResponse(
urlOrPredicate:
| string
| ((res: HTTPResponse) => boolean | Promise<boolean>),
options?: {
timeout?: number;
}
waitForResponse(
urlOrPredicate: string | AwaitablePredicate<HTTPResponse>,
options?: WaitTimeoutOptions
): Promise<HTTPResponse>;
}
```
## Parameters
| Parameter | Type | Description |
| -------------- | ------------------------------------------------------------------------------------------------------ | ---------------------------------------- |
| urlOrPredicate | string \| ((res: [HTTPResponse](./puppeteer.httpresponse.md)) =&gt; boolean \| Promise&lt;boolean&gt;) | A URL or predicate to wait for. |
| options | &#123; timeout?: number; &#125; | _(Optional)_ Optional waiting parameters |
| Parameter | Type | Description |
| -------------- | -------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
| urlOrPredicate | string \| [AwaitablePredicate](./puppeteer.awaitablepredicate.md)&lt;[HTTPResponse](./puppeteer.httpresponse.md)&gt; | A URL or predicate to wait for. |
| options | [WaitTimeoutOptions](./puppeteer.waittimeoutoptions.md) | _(Optional)_ Optional waiting parameters |
**Returns:**

View File

@ -50,6 +50,7 @@ import type {PDFOptions} from '../common/PDFOptions.js';
import {TimeoutSettings} from '../common/TimeoutSettings.js';
import type {
Awaitable,
AwaitablePredicate,
EvaluateFunc,
EvaluateFuncWith,
HandleFor,
@ -1652,12 +1653,30 @@ export abstract class Page extends EventEmitter<PageEvents> {
* pass `0` to disable the timeout. The default value can be changed by using
* the {@link Page.setDefaultTimeout} method.
*/
abstract waitForResponse(
urlOrPredicate:
| string
| ((res: HTTPResponse) => boolean | Promise<boolean>),
options?: {timeout?: number}
): Promise<HTTPResponse>;
waitForResponse(
urlOrPredicate: string | AwaitablePredicate<HTTPResponse>,
options: WaitTimeoutOptions = {}
): Promise<HTTPResponse> {
const {timeout: ms = this._timeoutSettings.timeout()} = options;
if (typeof urlOrPredicate === 'string') {
const url = urlOrPredicate;
urlOrPredicate = (request: HTTPResponse) => {
return request.url() === url;
};
}
const observable$ = fromEmitterEvent(this, PageEvent.Response).pipe(
filterAsync(urlOrPredicate),
raceWith(
timeout(ms),
fromEmitterEvent(this, PageEvent.Close).pipe(
map(() => {
throw new TargetCloseError('Page closed!');
})
)
)
);
return firstValueFrom(observable$);
}
/**
* @param options - Optional waiting parameters

View File

@ -718,22 +718,6 @@ export class BidiPage extends Page {
);
}
override async waitForResponse(
urlOrPredicate:
| string
| ((res: BidiHTTPResponse) => boolean | Promise<boolean>),
options: {timeout?: number} = {}
): Promise<BidiHTTPResponse> {
const {timeout = this._timeoutSettings.timeout()} = options;
return await waitForHTTP(
this.#networkManager,
NetworkManagerEvent.Response,
urlOrPredicate,
timeout,
this.#closedDeferred
);
}
override async waitForNetworkIdle(
options: {idleTime?: number; timeout?: number} = {}
): Promise<void> {

View File

@ -924,22 +924,6 @@ export class CdpPage extends Page {
);
}
override async waitForResponse(
urlOrPredicate:
| string
| ((res: HTTPResponse) => boolean | Promise<boolean>),
options: {timeout?: number} = {}
): Promise<HTTPResponse> {
const {timeout = this._timeoutSettings.timeout()} = options;
return await waitForHTTP(
this.#frameManager.networkManager,
NetworkManagerEvent.Response,
urlOrPredicate,
timeout,
this.#sessionCloseDeferred
);
}
override async waitForNetworkIdle(
options: {idleTime?: number; timeout?: number} = {}
): Promise<void> {

View File

@ -9,6 +9,11 @@ import type {JSHandle} from '../api/JSHandle.js';
import type {LazyArg} from './LazyArg.js';
/**
* @public
*/
export type AwaitablePredicate<T> = (value: T) => Awaitable<boolean>;
/**
* @public
*/