refactor: move target updates to the target manager (#10189)

This commit is contained in:
Alex Rudenko 2023-05-16 13:34:10 +02:00 committed by GitHub
parent 2e289a7e87
commit 372d12172b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 20 deletions

View File

@ -374,22 +374,11 @@ export class CDPBrowser extends BrowserBase {
}
};
#onTargetChanged = ({
target,
targetInfo,
}: {
target: Target;
targetInfo: Protocol.Target.TargetInfo;
}): void => {
const previousURL = target.url();
const wasInitialized = target._isInitialized;
target._targetInfoChanged(targetInfo);
if (wasInitialized && previousURL !== target.url()) {
this.emit(BrowserEmittedEvents.TargetChanged, target);
target
.browserContext()
.emit(BrowserContextEmittedEvents.TargetChanged, target);
}
#onTargetChanged = ({target}: {target: Target}): void => {
this.emit(BrowserEmittedEvents.TargetChanged, target);
target
.browserContext()
.emit(BrowserContextEmittedEvents.TargetChanged, target);
};
#onTargetDiscovered = (targetInfo: Protocol.Target.TargetInfo): void => {

View File

@ -263,10 +263,21 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
const target = this.#attachedTargetsByTargetId.get(
event.targetInfo.targetId
);
this.emit(TargetManagerEmittedEvents.TargetChanged, {
target: target!,
targetInfo: event.targetInfo,
});
if (!target) {
return;
}
const previousURL = target.url();
const wasInitialized = target._isInitialized;
target._targetInfoChanged(event.targetInfo);
if (wasInitialized && previousURL !== target.url()) {
this.emit(TargetManagerEmittedEvents.TargetChanged, {
target: target,
wasInitialized,
previousURL,
});
}
};
#onAttachedToTarget = async (

View File

@ -68,5 +68,8 @@ export const enum TargetManagerEmittedEvents {
TargetDiscovered = 'targetDiscovered',
TargetAvailable = 'targetAvailable',
TargetGone = 'targetGone',
/**
* Emitted after a target has been initialized and whenever its URL changes.
*/
TargetChanged = 'targetChanged',
}