MCPcopy
hub / github.com/foambubble/foam / VsCodeWatcher

Class VsCodeWatcher

packages/foam-vscode/src/vscode/services/watcher.ts:10–58  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8const DEBOUNCE_MS = 100;
9
10export class VsCodeWatcher implements IWatcher, IDisposable {
11 public onDidCreateEmitter = new Emitter<URI>();
12 public onDidChangeEmitter = new Emitter<URI>();
13 public onDidDeleteEmitter = new Emitter<URI>();
14 onDidCreate = this.onDidCreateEmitter.event;
15 onDidChange = this.onDidChangeEmitter.event;
16 onDidDelete = this.onDidDeleteEmitter.event;
17
18 private changeTimers = new Map<string, ReturnType<typeof setTimeout>>();
19
20 constructor(
21 private readonly vsCodeWatcher: FileSystemWatcher,
22 onDidSaveTextDocument?: Event<TextDocument>
23 ) {
24 vsCodeWatcher.onDidCreate(uri =>
25 this.onDidCreateEmitter.fire(fromVsCodeUri(uri))
26 );
27 vsCodeWatcher.onDidChange(uri =>
28 this.fireChange(fromVsCodeUri(uri))
29 );
30 vsCodeWatcher.onDidDelete(uri =>
31 this.onDidDeleteEmitter.fire(fromVsCodeUri(uri))
32 );
33 onDidSaveTextDocument?.(doc => this.fireChange(fromVsCodeUri(doc.uri)));
34 }
35
36 private fireChange(uri: URI): void {
37 const key = uri.path;
38 const existing = this.changeTimers.get(key);
39 if (existing) {
40 clearTimeout(existing);
41 }
42 this.changeTimers.set(
43 key,
44 setTimeout(() => {
45 this.changeTimers.delete(key);
46 this.onDidChangeEmitter.fire(uri);
47 }, DEBOUNCE_MS)
48 );
49 }
50
51 dispose(): void {
52 for (const timer of this.changeTimers.values()) {
53 clearTimeout(timer);
54 }
55 this.changeTimers.clear();
56 this.vsCodeWatcher.dispose();
57 }
58}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected