Skip to content

Commit

Permalink
Properly dispose of events in combineIterators() (Locators.iterEnvs()…
Browse files Browse the repository at this point in the history
…). (#15352)

The actual fix is in combineIterators().

Note that we also were not disposing of the event emitter in combineIterators(). This change removes the emitter, so it's a moot point.
  • Loading branch information
ericsnowcurrently authored Feb 10, 2021
1 parent d55e5f7 commit e04b43c
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/client/pythonEnvironments/base/locators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { EventEmitter } from 'vscode';
import { chain } from '../../common/utils/async';
import { Disposables } from '../../common/utils/resourceLifecycle';
import { PythonEnvInfo } from './info';
import { ILocator, IPythonEnvsIterator, PythonEnvUpdatedEvent, PythonLocatorQuery } from './locator';
import { PythonEnvsWatchers } from './watchers';
Expand All @@ -18,23 +18,27 @@ export function combineIterators(iterators: IPythonEnvsIterator[]): IPythonEnvsI
return result;
}

const emitter = new EventEmitter<PythonEnvUpdatedEvent | null>();
let numActive = events.length;
events.forEach((event) => {
event!((e: PythonEnvUpdatedEvent | null) => {
// NOSONAR
if (e === null) {
numActive -= 1;
if (numActive === 0) {
// All the sub-events are done so we're done.
emitter.fire(null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result.onUpdated = (handleEvent: (e: PythonEnvUpdatedEvent | null) => any) => {
const disposables = new Disposables();
let numActive = events.length;
events.forEach((event) => {
const disposable = event!((e: PythonEnvUpdatedEvent | null) => {
// NOSONAR
if (e === null) {
numActive -= 1;
if (numActive === 0) {
// All the sub-events are done so we're done.
handleEvent(null);
}
} else {
handleEvent(e);
}
} else {
emitter.fire(e);
}
});
disposables.push(disposable);
});
});
result.onUpdated = emitter.event;
return disposables;
};
return result;
}

Expand Down

0 comments on commit e04b43c

Please sign in to comment.