mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: use generic implementation for BrowserContext.waitForTarget (#11848)
Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com>
This commit is contained in:
parent
f62380d373
commit
27c71a9cf6
@ -12,7 +12,7 @@ This will look all open [browser contexts](./puppeteer.browsercontext.md).
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
class BrowserContext {
|
class BrowserContext {
|
||||||
abstract waitForTarget(
|
waitForTarget(
|
||||||
predicate: (x: Target) => boolean | Promise<boolean>,
|
predicate: (x: Target) => boolean | Promise<boolean>,
|
||||||
options?: WaitForTargetOptions
|
options?: WaitForTargetOptions
|
||||||
): Promise<Target>;
|
): Promise<Target>;
|
||||||
|
@ -4,8 +4,15 @@
|
|||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
filterAsync,
|
||||||
|
firstValueFrom,
|
||||||
|
from,
|
||||||
|
merge,
|
||||||
|
raceWith,
|
||||||
|
} from '../../third_party/rxjs/rxjs.js';
|
||||||
import {EventEmitter, type EventType} from '../common/EventEmitter.js';
|
import {EventEmitter, type EventType} from '../common/EventEmitter.js';
|
||||||
import {debugError} from '../common/util.js';
|
import {debugError, fromEmitterEvent, timeout} from '../common/util.js';
|
||||||
import {asyncDisposeSymbol, disposeSymbol} from '../util/disposable.js';
|
import {asyncDisposeSymbol, disposeSymbol} from '../util/disposable.js';
|
||||||
|
|
||||||
import type {Browser, Permission, WaitForTargetOptions} from './Browser.js';
|
import type {Browser, Permission, WaitForTargetOptions} from './Browser.js';
|
||||||
@ -108,10 +115,19 @@ export abstract class BrowserContext extends EventEmitter<BrowserContextEvents>
|
|||||||
* );
|
* );
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
abstract waitForTarget(
|
async waitForTarget(
|
||||||
predicate: (x: Target) => boolean | Promise<boolean>,
|
predicate: (x: Target) => boolean | Promise<boolean>,
|
||||||
options?: WaitForTargetOptions
|
options: WaitForTargetOptions = {}
|
||||||
): Promise<Target>;
|
): Promise<Target> {
|
||||||
|
const {timeout: ms = 30000} = options;
|
||||||
|
return await firstValueFrom(
|
||||||
|
merge(
|
||||||
|
fromEmitterEvent(this, BrowserContextEvent.TargetCreated),
|
||||||
|
fromEmitterEvent(this, BrowserContextEvent.TargetChanged),
|
||||||
|
from(this.targets())
|
||||||
|
).pipe(filterAsync(predicate), raceWith(timeout(ms)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list of all open {@link Page | pages} inside this
|
* Gets a list of all open {@link Page | pages} inside this
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
|
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
|
||||||
|
|
||||||
import type {WaitForTargetOptions} from '../api/Browser.js';
|
|
||||||
import {BrowserContext} from '../api/BrowserContext.js';
|
import {BrowserContext} from '../api/BrowserContext.js';
|
||||||
import type {Page} from '../api/Page.js';
|
import type {Page} from '../api/Page.js';
|
||||||
import type {Target} from '../api/Target.js';
|
import type {Target} from '../api/Target.js';
|
||||||
@ -53,15 +52,6 @@ export class BidiBrowserContext extends BrowserContext {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
override waitForTarget(
|
|
||||||
predicate: (x: Target) => boolean | Promise<boolean>,
|
|
||||||
options: WaitForTargetOptions = {}
|
|
||||||
): Promise<Target> {
|
|
||||||
return this.#browser.waitForTarget(target => {
|
|
||||||
return target.browserContext() === this && predicate(target);
|
|
||||||
}, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
get connection(): BidiConnection {
|
get connection(): BidiConnection {
|
||||||
return this.#connection;
|
return this.#connection;
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ import {
|
|||||||
type IsPageTargetCallback,
|
type IsPageTargetCallback,
|
||||||
type Permission,
|
type Permission,
|
||||||
type TargetFilterCallback,
|
type TargetFilterCallback,
|
||||||
type WaitForTargetOptions,
|
|
||||||
} from '../api/Browser.js';
|
} from '../api/Browser.js';
|
||||||
import {BrowserContext, BrowserContextEvent} from '../api/BrowserContext.js';
|
import {BrowserContext, BrowserContextEvent} from '../api/BrowserContext.js';
|
||||||
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
|
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
|
||||||
@ -451,15 +450,6 @@ export class CdpBrowserContext extends BrowserContext {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
override waitForTarget(
|
|
||||||
predicate: (x: Target) => boolean | Promise<boolean>,
|
|
||||||
options: WaitForTargetOptions = {}
|
|
||||||
): Promise<Target> {
|
|
||||||
return this.#browser.waitForTarget(target => {
|
|
||||||
return target.browserContext() === this && predicate(target);
|
|
||||||
}, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
override async pages(): Promise<Page[]> {
|
override async pages(): Promise<Page[]> {
|
||||||
const pages = await Promise.all(
|
const pages = await Promise.all(
|
||||||
this.targets()
|
this.targets()
|
||||||
|
Loading…
Reference in New Issue
Block a user