From 4a3b8b2d9eda2941769b53dad48b92d9c027d495 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 20 Jul 2023 13:00:44 +0200 Subject: [PATCH] refactor: separate target init from construction (#10600) --- .../src/common/ChromeTargetManager.ts | 20 ++++++++++++------- .../src/common/FirefoxTargetManager.ts | 5 +++-- packages/puppeteer-core/src/common/Target.ts | 7 ++++--- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/puppeteer-core/src/common/ChromeTargetManager.ts b/packages/puppeteer-core/src/common/ChromeTargetManager.ts index 5a9280beb3b..f80cbb842b1 100644 --- a/packages/puppeteer-core/src/common/ChromeTargetManager.ts +++ b/packages/puppeteer-core/src/common/ChromeTargetManager.ts @@ -227,6 +227,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { return; } const target = this.#targetFactory(event.targetInfo, undefined); + target._initialize(); this.#attachedTargetsByTargetId.set(event.targetInfo.targetId, target); } }; @@ -324,18 +325,12 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { return; } const target = this.#targetFactory(targetInfo); + target._initialize(); this.#attachedTargetsByTargetId.set(targetInfo.targetId, target); this.emit(TargetManagerEmittedEvents.TargetAvailable, target); return; } - if (this.#targetFilterCallback && !this.#targetFilterCallback(targetInfo)) { - this.#ignoredTargets.add(targetInfo.targetId); - this.#finishInitializationIfReady(targetInfo.targetId); - await silentDetach(); - return; - } - const existingTarget = this.#attachedTargetsByTargetId.has( targetInfo.targetId ); @@ -344,6 +339,17 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager { ? this.#attachedTargetsByTargetId.get(targetInfo.targetId)! : this.#targetFactory(targetInfo, session); + if (this.#targetFilterCallback && !this.#targetFilterCallback(targetInfo)) { + this.#ignoredTargets.add(targetInfo.targetId); + this.#finishInitializationIfReady(targetInfo.targetId); + await silentDetach(); + return; + } + + if (!existingTarget) { + target._initialize(); + } + this.#setupAttachmentListeners(session); if (existingTarget) { diff --git a/packages/puppeteer-core/src/common/FirefoxTargetManager.ts b/packages/puppeteer-core/src/common/FirefoxTargetManager.ts index 1a095a8ff49..b798180c8e4 100644 --- a/packages/puppeteer-core/src/common/FirefoxTargetManager.ts +++ b/packages/puppeteer-core/src/common/FirefoxTargetManager.ts @@ -187,11 +187,13 @@ export class FirefoxTargetManager if (event.targetInfo.type === 'browser' && event.targetInfo.attached) { const target = this.#targetFactory(event.targetInfo, undefined); + target._initialize(); this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target); this.#finishInitializationIfReady(target._targetId); return; } + const target = this.#targetFactory(event.targetInfo, undefined); if ( this.#targetFilterCallback && !this.#targetFilterCallback(event.targetInfo) @@ -200,8 +202,7 @@ export class FirefoxTargetManager this.#finishInitializationIfReady(event.targetInfo.targetId); return; } - - const target = this.#targetFactory(event.targetInfo, undefined); + target._initialize(); this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target); this.emit(TargetManagerEmittedEvents.TargetAvailable, target); this.#finishInitializationIfReady(target._targetId); diff --git a/packages/puppeteer-core/src/common/Target.ts b/packages/puppeteer-core/src/common/Target.ts index 4a9745e6741..559cd1d95aa 100644 --- a/packages/puppeteer-core/src/common/Target.ts +++ b/packages/puppeteer-core/src/common/Target.ts @@ -66,6 +66,8 @@ export class Target { _targetId: string; /** + * To initialize the target for use, call initialize. + * * @internal */ constructor( @@ -81,7 +83,6 @@ export class Target { this.#browserContext = browserContext; this._targetId = targetInfo.targetId; this.#sessionFactory = sessionFactory; - this._initialize(); } /** @@ -197,7 +198,7 @@ export class Target { /** * @internal */ - protected _initialize(): void { + _initialize(): void { this._initializedDeferred.resolve(InitializationStatus.SUCCESS); } @@ -247,7 +248,7 @@ export class PageTarget extends Target { this.#screenshotTaskQueue = screenshotTaskQueue; } - protected override _initialize(): void { + override _initialize(): void { this._initializedDeferred .valueOrThrow() .then(async result => {