forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchers.ts
More file actions
66 lines (58 loc) · 1.87 KB
/
watchers.ts
File metadata and controls
66 lines (58 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Disposable, Event } from 'vscode';
import { IPythonEnvsWatcher, PythonEnvsChangedEvent, PythonEnvsWatcher } from './watcher';
/**
* A wrapper around a set of watchers, exposing them as a single watcher.
*
* If any of the wrapped watchers emits an event then this wrapper
* emits that event.
*/
export class PythonEnvsWatchers implements IPythonEnvsWatcher {
public readonly onChanged: Event<PythonEnvsChangedEvent>;
private watcher = new PythonEnvsWatcher();
constructor(watchers: ReadonlyArray<IPythonEnvsWatcher>) {
this.onChanged = this.watcher.onChanged;
watchers.forEach((w) => {
w.onChanged((e) => this.watcher.fire(e));
});
}
}
// This matches the `vscode.Event` arg.
type EnvsEventListener = (e: PythonEnvsChangedEvent) => unknown;
/**
* A watcher wrapper that can be disabled.
*
* If disabled, events emitted by the wrapped watcher are discarded.
*/
export class DisableableEnvsWatcher implements IPythonEnvsWatcher {
protected enabled = true;
constructor(
// To wrap more than one use `PythonEnvWatchers`.
private readonly wrapped: IPythonEnvsWatcher
) {}
/**
* Ensure that the watcher is enabled.
*/
public enable() {
this.enabled = true;
}
/**
* Ensure that the watcher is disabled.
*/
public disable() {
this.enabled = false;
}
// This matches the signature of `vscode.Event`.
public onChanged(listener: EnvsEventListener, thisArgs?: unknown, disposables?: Disposable[]): Disposable {
return this.wrapped.onChanged(
(e: PythonEnvsChangedEvent) => {
if (this.enabled) {
listener(e);
}
},
thisArgs,
disposables
);
}
}