refactor: separate target init from construction (#10600)

This commit is contained in:
Alex Rudenko 2023-07-20 13:00:44 +02:00 committed by GitHub
parent 87522e778a
commit 4a3b8b2d9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 12 deletions

View File

@ -227,6 +227,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
return; return;
} }
const target = this.#targetFactory(event.targetInfo, undefined); const target = this.#targetFactory(event.targetInfo, undefined);
target._initialize();
this.#attachedTargetsByTargetId.set(event.targetInfo.targetId, target); this.#attachedTargetsByTargetId.set(event.targetInfo.targetId, target);
} }
}; };
@ -324,18 +325,12 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
return; return;
} }
const target = this.#targetFactory(targetInfo); const target = this.#targetFactory(targetInfo);
target._initialize();
this.#attachedTargetsByTargetId.set(targetInfo.targetId, target); this.#attachedTargetsByTargetId.set(targetInfo.targetId, target);
this.emit(TargetManagerEmittedEvents.TargetAvailable, target); this.emit(TargetManagerEmittedEvents.TargetAvailable, target);
return; return;
} }
if (this.#targetFilterCallback && !this.#targetFilterCallback(targetInfo)) {
this.#ignoredTargets.add(targetInfo.targetId);
this.#finishInitializationIfReady(targetInfo.targetId);
await silentDetach();
return;
}
const existingTarget = this.#attachedTargetsByTargetId.has( const existingTarget = this.#attachedTargetsByTargetId.has(
targetInfo.targetId targetInfo.targetId
); );
@ -344,6 +339,17 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
? this.#attachedTargetsByTargetId.get(targetInfo.targetId)! ? this.#attachedTargetsByTargetId.get(targetInfo.targetId)!
: this.#targetFactory(targetInfo, session); : 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); this.#setupAttachmentListeners(session);
if (existingTarget) { if (existingTarget) {

View File

@ -187,11 +187,13 @@ export class FirefoxTargetManager
if (event.targetInfo.type === 'browser' && event.targetInfo.attached) { if (event.targetInfo.type === 'browser' && event.targetInfo.attached) {
const target = this.#targetFactory(event.targetInfo, undefined); const target = this.#targetFactory(event.targetInfo, undefined);
target._initialize();
this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target); this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target);
this.#finishInitializationIfReady(target._targetId); this.#finishInitializationIfReady(target._targetId);
return; return;
} }
const target = this.#targetFactory(event.targetInfo, undefined);
if ( if (
this.#targetFilterCallback && this.#targetFilterCallback &&
!this.#targetFilterCallback(event.targetInfo) !this.#targetFilterCallback(event.targetInfo)
@ -200,8 +202,7 @@ export class FirefoxTargetManager
this.#finishInitializationIfReady(event.targetInfo.targetId); this.#finishInitializationIfReady(event.targetInfo.targetId);
return; return;
} }
target._initialize();
const target = this.#targetFactory(event.targetInfo, undefined);
this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target); this.#availableTargetsByTargetId.set(event.targetInfo.targetId, target);
this.emit(TargetManagerEmittedEvents.TargetAvailable, target); this.emit(TargetManagerEmittedEvents.TargetAvailable, target);
this.#finishInitializationIfReady(target._targetId); this.#finishInitializationIfReady(target._targetId);

View File

@ -66,6 +66,8 @@ export class Target {
_targetId: string; _targetId: string;
/** /**
* To initialize the target for use, call initialize.
*
* @internal * @internal
*/ */
constructor( constructor(
@ -81,7 +83,6 @@ export class Target {
this.#browserContext = browserContext; this.#browserContext = browserContext;
this._targetId = targetInfo.targetId; this._targetId = targetInfo.targetId;
this.#sessionFactory = sessionFactory; this.#sessionFactory = sessionFactory;
this._initialize();
} }
/** /**
@ -197,7 +198,7 @@ export class Target {
/** /**
* @internal * @internal
*/ */
protected _initialize(): void { _initialize(): void {
this._initializedDeferred.resolve(InitializationStatus.SUCCESS); this._initializedDeferred.resolve(InitializationStatus.SUCCESS);
} }
@ -247,7 +248,7 @@ export class PageTarget extends Target {
this.#screenshotTaskQueue = screenshotTaskQueue; this.#screenshotTaskQueue = screenshotTaskQueue;
} }
protected override _initialize(): void { override _initialize(): void {
this._initializedDeferred this._initializedDeferred
.valueOrThrow() .valueOrThrow()
.then(async result => { .then(async result => {