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 = ({ #onTargetChanged = ({target}: {target: Target}): void => {
target, this.emit(BrowserEmittedEvents.TargetChanged, target);
targetInfo, target
}: { .browserContext()
target: Target; .emit(BrowserContextEmittedEvents.TargetChanged, 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);
}
}; };
#onTargetDiscovered = (targetInfo: Protocol.Target.TargetInfo): void => { #onTargetDiscovered = (targetInfo: Protocol.Target.TargetInfo): void => {

View File

@ -263,10 +263,21 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
const target = this.#attachedTargetsByTargetId.get( const target = this.#attachedTargetsByTargetId.get(
event.targetInfo.targetId event.targetInfo.targetId
); );
this.emit(TargetManagerEmittedEvents.TargetChanged, { if (!target) {
target: target!, return;
targetInfo: event.targetInfo, }
}); 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 ( #onAttachedToTarget = async (

View File

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