mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: use generic implementation for waitForResponse
(#11752)
This commit is contained in:
parent
3a4a23faa8
commit
3698fa8aec
@ -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) | |
|
||||
|
13
docs/api/puppeteer.awaitablepredicate.md
Normal file
13
docs/api/puppeteer.awaitablepredicate.md
Normal 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)
|
@ -8,13 +8,9 @@ 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>;
|
||||
}
|
||||
```
|
||||
@ -22,9 +18,9 @@ class Page {
|
||||
## Parameters
|
||||
|
||||
| Parameter | Type | Description |
|
||||
| -------------- | ------------------------------------------------------------------------------------------------------ | ---------------------------------------- |
|
||||
| urlOrPredicate | string \| ((res: [HTTPResponse](./puppeteer.httpresponse.md)) => boolean \| Promise<boolean>) | A URL or predicate to wait for. |
|
||||
| options | { timeout?: number; } | _(Optional)_ Optional waiting parameters |
|
||||
| -------------- | -------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
|
||||
| urlOrPredicate | string \| [AwaitablePredicate](./puppeteer.awaitablepredicate.md)<[HTTPResponse](./puppeteer.httpresponse.md)> | A URL or predicate to wait for. |
|
||||
| options | [WaitTimeoutOptions](./puppeteer.waittimeoutoptions.md) | _(Optional)_ Optional waiting parameters |
|
||||
|
||||
**Returns:**
|
||||
|
||||
|
@ -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
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user