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;
}
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) {

View File

@ -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);

View File

@ -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 => {