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) | | | [ActionResult](./puppeteer.actionresult.md) | |
| [Awaitable](./puppeteer.awaitable.md) | | | [Awaitable](./puppeteer.awaitable.md) | |
| [AwaitableIterable](./puppeteer.awaitableiterable.md) | | | [AwaitableIterable](./puppeteer.awaitableiterable.md) | |
| [AwaitablePredicate](./puppeteer.awaitablepredicate.md) | |
| [AwaitedLocator](./puppeteer.awaitedlocator.md) | | | [AwaitedLocator](./puppeteer.awaitedlocator.md) | |
| [CDPEvents](./puppeteer.cdpevents.md) | | | [CDPEvents](./puppeteer.cdpevents.md) | |
| [ChromeReleaseChannel](./puppeteer.chromereleasechannel.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 ```typescript
class Page { class Page {
abstract waitForResponse( waitForResponse(
urlOrPredicate: urlOrPredicate: string | AwaitablePredicate<HTTPResponse>,
| string options?: WaitTimeoutOptions
| ((res: HTTPResponse) => boolean | Promise<boolean>),
options?: {
timeout?: number;
}
): Promise<HTTPResponse>; ): Promise<HTTPResponse>;
} }
``` ```
## Parameters ## Parameters
| Parameter | Type | Description | | Parameter | Type | Description |
| -------------- | ------------------------------------------------------------------------------------------------------ | ---------------------------------------- | | -------------- | -------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
| urlOrPredicate | string \| ((res: [HTTPResponse](./puppeteer.httpresponse.md)) =&gt; boolean \| Promise&lt;boolean&gt;) | A URL or predicate to wait for. | | urlOrPredicate | string \| [AwaitablePredicate](./puppeteer.awaitablepredicate.md)&lt;[HTTPResponse](./puppeteer.httpresponse.md)&gt; | A URL or predicate to wait for. |
| options | &#123; timeout?: number; &#125; | _(Optional)_ Optional waiting parameters | | options | [WaitTimeoutOptions](./puppeteer.waittimeoutoptions.md) | _(Optional)_ Optional waiting parameters |
**Returns:** **Returns:**

View File

@ -50,6 +50,7 @@ import type {PDFOptions} from '../common/PDFOptions.js';
import {TimeoutSettings} from '../common/TimeoutSettings.js'; import {TimeoutSettings} from '../common/TimeoutSettings.js';
import type { import type {
Awaitable, Awaitable,
AwaitablePredicate,
EvaluateFunc, EvaluateFunc,
EvaluateFuncWith, EvaluateFuncWith,
HandleFor, 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 * pass `0` to disable the timeout. The default value can be changed by using
* the {@link Page.setDefaultTimeout} method. * the {@link Page.setDefaultTimeout} method.
*/ */
abstract waitForResponse( waitForResponse(
urlOrPredicate: urlOrPredicate: string | AwaitablePredicate<HTTPResponse>,
| string options: WaitTimeoutOptions = {}
| ((res: HTTPResponse) => boolean | Promise<boolean>), ): Promise<HTTPResponse> {
options?: {timeout?: number} const {timeout: ms = this._timeoutSettings.timeout()} = options;
): Promise<HTTPResponse>; 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 * @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( override async waitForNetworkIdle(
options: {idleTime?: number; timeout?: number} = {} options: {idleTime?: number; timeout?: number} = {}
): Promise<void> { ): 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( override async waitForNetworkIdle(
options: {idleTime?: number; timeout?: number} = {} options: {idleTime?: number; timeout?: number} = {}
): Promise<void> { ): Promise<void> {

View File

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