Skip to content

Commit

Permalink
fix: resolve target manager init if no existing targets detected
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Aug 5, 2022
1 parent eb64709 commit 1a32cb2
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/common/ChromeTargetManager.ts
Expand Up @@ -103,30 +103,37 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {

// TODO: remove `as any` once the protocol definitions are updated with the
// next Chromium roll.
this.#connection.send('Target.setDiscoverTargets', {
discover: true,
filter: [{type: 'tab', exclude: true}, {}],
} as any);
this.#connection
.send('Target.setDiscoverTargets', {
discover: true,
filter: [{type: 'tab', exclude: true}, {}],
} as any)
.then(this.#storeExistingTargetsForInit)
.catch(debugError);
}

async initialize(): Promise<void> {
this.#targetsIdsForInit = new Set();
#storeExistingTargetsForInit = () => {
for (const [
targetId,
targetInfo,
] of this.#discoveredTargetsByTargetId.entries()) {
if (
!this.#targetFilterCallback ||
this.#targetFilterCallback(targetInfo)
(!this.#targetFilterCallback ||
this.#targetFilterCallback(targetInfo)) &&
targetInfo.type !== 'browser'
) {
this.#targetsIdsForInit.add(targetId);
}
}
};

async initialize(): Promise<void> {
await this.#connection.send('Target.setAutoAttach', {
waitForDebuggerOnStart: true,
flatten: true,
autoAttach: true,
});
this.#finishInitializationIfReady();
await this.#initializePromise;
}

Expand Down Expand Up @@ -358,9 +365,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {

this.#targetsIdsForInit.delete(target._targetId);
this.emit(TargetManagerEmittedEvents.TargetAvailable, target);
if (this.#targetsIdsForInit.size === 0) {
this.#initializeCallback();
}
this.#finishInitializationIfReady();

// TODO: the browser might be shutting down here. What do we do with the
// error?
Expand All @@ -374,8 +379,8 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
]).catch(debugError);
};

#finishInitializationIfReady(targetId: string): void {
this.#targetsIdsForInit.delete(targetId);
#finishInitializationIfReady(targetId?: string): void {
targetId !== undefined && this.#targetsIdsForInit.delete(targetId);
if (this.#targetsIdsForInit.size === 0) {
this.#initializeCallback();
}
Expand Down

0 comments on commit 1a32cb2

Please sign in to comment.