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

Class NodeWatcher

packages/foam-cli/src/support/watcher.ts:14–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12 * with so the watcher and the in-memory graph stay in sync.
13 */
14export class NodeWatcher implements IWatcher, IDisposable {
15 public onDidCreateEmitter = new Emitter<URI>();
16 public onDidChangeEmitter = new Emitter<URI>();
17 public onDidDeleteEmitter = new Emitter<URI>();
18 onDidCreate = this.onDidCreateEmitter.event;
19 onDidChange = this.onDidChangeEmitter.event;
20 onDidDelete = this.onDidDeleteEmitter.event;
21
22 private readonly fsWatcher: FSWatcher;
23 private readonly changeTimers = new Map<string, ReturnType<typeof setTimeout>>();
24
25 constructor(
26 paths: string[] | string,
27 options: { ignored?: (string | RegExp)[] } = {}
28 ) {
29 this.fsWatcher = chokidar.watch(paths, {
30 ignored: options.ignored,
31 // Don't fire `add` for files that already exist when the watcher starts —
32 // the workspace bootstrap already loaded them.
33 ignoreInitial: true,
34 persistent: true,
35 awaitWriteFinish: {
36 stabilityThreshold: 50,
37 pollInterval: 25,
38 },
39 });
40
41 this.fsWatcher.on('add', filePath =>
42 this.onDidCreateEmitter.fire(URI.file(filePath))
43 );
44 this.fsWatcher.on('change', filePath =>
45 this.fireChange(URI.file(filePath))
46 );
47 this.fsWatcher.on('unlink', filePath =>
48 this.onDidDeleteEmitter.fire(URI.file(filePath))
49 );
50 }
51
52 private fireChange(uri: URI): void {
53 const key = uri.path;
54 const existing = this.changeTimers.get(key);
55 if (existing) {
56 clearTimeout(existing);
57 }
58 this.changeTimers.set(
59 key,
60 setTimeout(() => {
61 this.changeTimers.delete(key);
62 this.onDidChangeEmitter.fire(uri);
63 }, DEBOUNCE_MS)
64 );
65 }
66
67 async dispose(): Promise<void> {
68 for (const timer of this.changeTimers.values()) {
69 clearTimeout(timer);
70 }
71 this.changeTimers.clear();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected