chore: add waitForInitiallyDiscoveredTargets (#10745)

This commit is contained in:
Alex Rudenko 2023-08-17 12:48:46 +02:00 committed by GitHub
parent fc08a7dd54
commit 77ffbba141
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -64,7 +64,8 @@ export class CDPBrowser extends BrowserBase {
process?: ChildProcess,
closeCallback?: BrowserCloseCallback,
targetFilterCallback?: TargetFilterCallback,
isPageTargetCallback?: IsPageTargetCallback
isPageTargetCallback?: IsPageTargetCallback,
waitForInitiallyDiscoveredTargets = true
): Promise<CDPBrowser> {
const browser = new CDPBrowser(
product,
@ -75,7 +76,8 @@ export class CDPBrowser extends BrowserBase {
process,
closeCallback,
targetFilterCallback,
isPageTargetCallback
isPageTargetCallback,
waitForInitiallyDiscoveredTargets
);
await browser._attach();
return browser;
@ -111,7 +113,8 @@ export class CDPBrowser extends BrowserBase {
process?: ChildProcess,
closeCallback?: BrowserCloseCallback,
targetFilterCallback?: TargetFilterCallback,
isPageTargetCallback?: IsPageTargetCallback
isPageTargetCallback?: IsPageTargetCallback,
waitForInitiallyDiscoveredTargets = true
) {
super();
product = product || 'chrome';
@ -137,7 +140,8 @@ export class CDPBrowser extends BrowserBase {
this.#targetManager = new ChromeTargetManager(
connection,
this.#createTarget,
this.#targetFilterCallback
this.#targetFilterCallback,
waitForInitiallyDiscoveredTargets
);
}
this.#defaultContext = new CDPBrowserContext(this.#connection, this);

View File

@ -84,16 +84,19 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
#initializeDeferred = Deferred.create<void>();
#targetsIdsForInit = new Set<string>();
#waitForInitiallyDiscoveredTargets = true;
constructor(
connection: Connection,
targetFactory: TargetFactory,
targetFilterCallback?: TargetFilterCallback
targetFilterCallback?: TargetFilterCallback,
waitForInitiallyDiscoveredTargets = true
) {
super();
this.#connection = connection;
this.#targetFilterCallback = targetFilterCallback;
this.#targetFactory = targetFactory;
this.#waitForInitiallyDiscoveredTargets = waitForInitiallyDiscoveredTargets;
this.#connection.on('Target.targetCreated', this.#onTargetCreated);
this.#connection.on('Target.targetDestroyed', this.#onTargetDestroyed);
@ -111,6 +114,9 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
}
#storeExistingTargetsForInit = () => {
if (!this.#waitForInitiallyDiscoveredTargets) {
return;
}
for (const [
targetId,
targetInfo,