-
Notifications
You must be signed in to change notification settings - Fork 429
/
watcher.ts
76 lines (64 loc) · 2.81 KB
/
watcher.ts
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
67
68
69
70
71
72
73
74
75
76
import * as vscode from 'vscode';
class EventDispatcher implements vscode.Disposable {
changeEvent = new vscode.EventEmitter<vscode.Uri>();
deleteEvent = new vscode.EventEmitter<vscode.Uri>();
createEvent = new vscode.EventEmitter<vscode.Uri>();
disposeIndividualWatcherEvent = new vscode.EventEmitter<IndividualWatcher>();
dispose() {
this.createEvent.dispose();
this.deleteEvent.dispose();
this.changeEvent.dispose();
this.disposeIndividualWatcherEvent.dispose();
}
}
export class IndividualWatcher implements vscode.Disposable {
private readonly _watcher = vscode.workspace.createFileSystemWatcher(this._pattern);
private readonly _changeSub = this._watcher.onDidChange(e => this._disp.changeEvent.fire(e));
private readonly _delSub = this._watcher.onDidDelete(e => this._disp.deleteEvent.fire(e));
private readonly _createSub = this._watcher.onDidCreate(e => this._disp.createEvent.fire(e));
constructor(private readonly _disp: EventDispatcher, private readonly _pattern: string) {}
dispose() {
this._changeSub.dispose();
this._delSub.dispose();
this._createSub.dispose();
this._watcher.dispose();
this._disp.disposeIndividualWatcherEvent.fire(this);
}
}
export class MultiWatcher implements vscode.Disposable {
private readonly _watchers = new Set<IndividualWatcher>();
private readonly _dispatcher = new EventDispatcher();
private readonly _anyEventEmitter = new vscode.EventEmitter<vscode.Uri>();
private readonly _unregisterSub
= this._dispatcher.disposeIndividualWatcherEvent.event(indiv => { this._watchers.delete(indiv); });
private readonly _createSub = this.onCreate(e => this._anyEventEmitter.fire(e));
private readonly _delSub = this.onDelete(e => this._anyEventEmitter.fire(e));
private readonly _changeSub = this.onChange(e => this._anyEventEmitter.fire(e));
constructor(...patterns: string[]) {
for (const pattern of patterns) {
this.createWatcher(pattern);
}
}
dispose() {
this._changeSub.dispose();
this._delSub.dispose();
this._createSub.dispose();
// We copy the watchers into an array so we can modify the set
for (const indiv of Array.from(this._watchers)) {
indiv.dispose();
}
console.assert(this._watchers.size == 0, 'Expected disposal of individual filesystem watchers');
this._unregisterSub.dispose();
this._anyEventEmitter.dispose();
this._dispatcher.dispose();
}
get onChange() { return this._dispatcher.changeEvent.event; }
get onDelete() { return this._dispatcher.deleteEvent.event; }
get onCreate() { return this._dispatcher.createEvent.event; }
get onAnyEvent() { return this._anyEventEmitter.event; }
createWatcher(pattern: string): vscode.Disposable {
const indiv = new IndividualWatcher(this._dispatcher, pattern);
this._watchers.add(indiv);
return indiv;
}
}