-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathfile-watching.ts
59 lines (50 loc) · 1.54 KB
/
file-watching.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
type BuilderWatcherCallback = (
events: Array<{ path: string; type: 'created' | 'modified' | 'deleted'; time?: number }>,
) => void;
interface BuilderWatcherFactory {
watch(
files: Iterable<string>,
directories: Iterable<string>,
callback: BuilderWatcherCallback,
): { close(): void };
}
class WatcherDescriptor {
constructor(
readonly files: ReadonlySet<string>,
readonly directories: ReadonlySet<string>,
readonly callback: BuilderWatcherCallback,
) {}
shouldNotify(path: string): boolean {
return true;
}
}
export class WatcherNotifier implements BuilderWatcherFactory {
private readonly descriptors = new Set<WatcherDescriptor>();
notify(events: Iterable<{ path: string; type: 'modified' | 'deleted' }>): void {
for (const descriptor of this.descriptors) {
for (const { path } of events) {
if (descriptor.shouldNotify(path)) {
descriptor.callback([...events]);
break;
}
}
}
}
watch(
files: Iterable<string>,
directories: Iterable<string>,
callback: BuilderWatcherCallback,
): { close(): void } {
const descriptor = new WatcherDescriptor(new Set(files), new Set(directories), callback);
this.descriptors.add(descriptor);
return { close: () => this.descriptors.delete(descriptor) };
}
}
export type { BuilderWatcherFactory };