Skip to content

Commit e04b43c

Browse files
author
Eric Snow
authored
Properly dispose of events in combineIterators() (Locators.iterEnvs()). (microsoft#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.
1 parent d55e5f7 commit e04b43c

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

src/client/pythonEnvironments/base/locators.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

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

21-
const emitter = new EventEmitter<PythonEnvUpdatedEvent | null>();
22-
let numActive = events.length;
23-
events.forEach((event) => {
24-
event!((e: PythonEnvUpdatedEvent | null) => {
25-
// NOSONAR
26-
if (e === null) {
27-
numActive -= 1;
28-
if (numActive === 0) {
29-
// All the sub-events are done so we're done.
30-
emitter.fire(null);
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
result.onUpdated = (handleEvent: (e: PythonEnvUpdatedEvent | null) => any) => {
23+
const disposables = new Disposables();
24+
let numActive = events.length;
25+
events.forEach((event) => {
26+
const disposable = event!((e: PythonEnvUpdatedEvent | null) => {
27+
// NOSONAR
28+
if (e === null) {
29+
numActive -= 1;
30+
if (numActive === 0) {
31+
// All the sub-events are done so we're done.
32+
handleEvent(null);
33+
}
34+
} else {
35+
handleEvent(e);
3136
}
32-
} else {
33-
emitter.fire(e);
34-
}
37+
});
38+
disposables.push(disposable);
3539
});
36-
});
37-
result.onUpdated = emitter.event;
40+
return disposables;
41+
};
3842
return result;
3943
}
4044

0 commit comments

Comments
 (0)