This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.ts
171 lines (126 loc) · 3.64 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Std } from "./deps.ts";
type WatchEventContent =
| { type: "create"; path: string }
| { type: "modify"; path: string }
| { type: "remove"; path: string };
export type WatchEventType = WatchEventContent["type"];
export class WatchEvent extends Event {
readonly path: string;
constructor({ type, path }: WatchEventContent) {
super(type);
this.path = path;
}
}
export class Watcher extends EventTarget {
public readonly target: string;
public readonly recursive: boolean;
private readonly fswatcher: Deno.FsWatcher;
private readonly emitter: AsyncIterableIterator<Deno.FsEvent>;
public readonly abort: () => void;
private readonly signal: Promise<void>;
constructor(path = "", recursive = true) {
super();
const target = Std.path.resolve(path);
this.target = target;
this.recursive = recursive;
this.fswatcher = Deno.watchFs(target, { recursive });
this.emitter = this.fswatcher[Symbol.asyncIterator]();
let abort: () => void;
this.signal = new Promise<void>((resolve) => (abort = resolve));
this.abort = abort!;
}
// --- --- --- --- --- --- --- --- ---
public readonly watch = async () => {
let event;
while ((event = await this.maybeEmit())) {
if (event.done === true) break;
this.handle(event.value);
}
this.cleanup();
};
private readonly maybeEmit = () =>
Promise.race([this.emitter.next(), this.signal]);
private readonly cleanup = () => {
this.fswatcher.close();
for (const state of Object.values(this.states)) {
state.cleanup();
}
};
// --- --- --- --- --- --- --- --- ---
private readonly handle = (event: Deno.FsEvent) => {
if (event.flag) return console.warn(`detected flag: "${event.flag}"`);
if (event.paths.length !== 1) return;
const [path] = event.paths;
const kind = event.kind;
switch (kind) {
case "any":
case "access":
case "other":
return;
}
this.process(path, kind);
};
// --- --- --- --- --- --- --- --- ---
private readonly process = (path: string, kind: WatchEventType) => {
switch (kind) {
case "create":
case "remove":
this.dispatch({ type: kind, path });
break;
case "modify":
this.states.modify.update(path);
break;
}
};
private states = {
modify: new WatcherState((path) => this.dispatch({ type: "modify", path })),
};
private readonly dispatch = (content: WatchEventContent) =>
this.dispatchEvent(new WatchEvent(content));
}
class WatcherState {
private static readonly THRESHOLD: number = 4;
private readonly callback;
constructor(callback: (path: string) => void) {
this.callback = callback;
}
private timeout?: number;
private path?: string;
public readonly update = (path: string) => {
if (!this.path) {
this.set(path);
return;
}
if (this.path === path) {
this.clear();
this.set(path);
return;
}
if (this.path !== path) {
this.invoke();
this.set(path);
return;
}
throw new UnreachableError();
};
private readonly set = (path: string) => {
this.timeout = setTimeout(this.invoke, WatcherState.THRESHOLD);
this.path = path;
};
private readonly clear = () => {
clearTimeout(this.timeout);
this.timeout = undefined;
this.path = undefined;
};
private readonly invoke = () => {
if (!this.path || !this.timeout) throw new UnreachableError();
this.callback(this.path);
this.clear();
};
public readonly cleanup = () => this.clear();
}
class UnreachableError extends Error {
constructor() {
super("entered unreachable code");
}
}