Skip to content

Commit c551b16

Browse files
Add a disableable watcher wrapper.
1 parent 8e9e079 commit c551b16

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/client/pythonEnvironments/base/watchers.ts

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

4-
import { Event } from 'vscode';
4+
import { Disposable, Event } from 'vscode';
5+
import { Disableable } from '../../common/utils/misc';
56
import { IPythonEnvsWatcher, PythonEnvsChangedEvent, PythonEnvsWatcher } from './watcher';
67

78
/**
@@ -21,3 +22,33 @@ export class PythonEnvsWatchers {
2122
});
2223
}
2324
}
25+
26+
// tslint:disable-next-line:no-any
27+
type EnvsEventListener = (e: PythonEnvsChangedEvent) => any;
28+
29+
/**
30+
* A watcher wrapper that can be disabled.
31+
*
32+
* If disabled, events emitted by the wrapped watcher are discarded.
33+
*/
34+
export class DisableableEnvsWatcher extends Disableable {
35+
constructor(
36+
// To wrap more than one use `PythonEnvWatchers`.
37+
private readonly wrapped: IPythonEnvsWatcher
38+
) {
39+
super();
40+
}
41+
42+
// tslint:disable-next-line:no-any
43+
public onChanged(listener: EnvsEventListener, thisArgs?: any, disposables?: Disposable[]): Disposable {
44+
return this.wrapped.onChanged(
45+
(e: PythonEnvsChangedEvent) => {
46+
if (this.isEnabled) {
47+
listener(e);
48+
}
49+
},
50+
thisArgs,
51+
disposables
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)