-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileWatcher.ts
36 lines (34 loc) · 977 Bytes
/
fileWatcher.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
import { successLog } from "https://deno.land/x/colorlog/mod.ts";
export default class Watcher {
changed: boolean = false;
constructor(dir: string) {
successLog(`[+] starting file watcher in directory ${dir}`);
this.init(dir);
}
/**
* top-level await seems to misbehave in constructor..
* @param x path to watch
*/
private async init(x: string) {
const watcher = Deno.watchFs(x);
for await (const event of watcher) {
this.changed = true;
successLog(
`[+] models updated. The next request will receive the changes (${event.kind})`,
);
}
}
/**
* Check if the watcher witnessed any changes
*/
public updated(): boolean {
return this.changed;
}
/**
* Acknowledge the change, causing the changed value to be false to accept other changes
* note: don't ack if you want to trigger an update each request (updated() always returns true)
*/
public ack() {
this.changed = false;
}
}