From 77ffbba141cadb96b8f9c9d85c21334a11a7dbc2 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 17 Aug 2023 12:48:46 +0200 Subject: [PATCH] chore: add waitForInitiallyDiscoveredTargets (#10745) --- packages/puppeteer-core/src/common/Browser.ts | 12 ++++++++---- .../puppeteer-core/src/common/ChromeTargetManager.ts | 8 +++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/puppeteer-core/src/common/Browser.ts b/packages/puppeteer-core/src/common/Browser.ts index 6a0df709..b2d0f2af 100644 --- a/packages/puppeteer-core/src/common/Browser.ts +++ b/packages/puppeteer-core/src/common/Browser.ts @@ -64,7 +64,8 @@ export class CDPBrowser extends BrowserBase { process?: ChildProcess, closeCallback?: BrowserCloseCallback, targetFilterCallback?: TargetFilterCallback, - isPageTargetCallback?: IsPageTargetCallback + isPageTargetCallback?: IsPageTargetCallback, + waitForInitiallyDiscoveredTargets = true ): Promise { 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); diff --git a/packages/puppeteer-core/src/common/ChromeTargetManager.ts b/packages/puppeteer-core/src/common/ChromeTargetManager.ts index e31e14f2..edd29b39 100644 --- a/packages/puppeteer-core/src/common/ChromeTargetManager.ts +++ b/packages/puppeteer-core/src/common/ChromeTargetManager.ts @@ -84,16 +84,19 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { #initializeDeferred = Deferred.create(); #targetsIdsForInit = new Set(); + #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,