-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathfile-watching.ts
52 lines (44 loc) · 1.34 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
/**
* @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.io/license
*/
import {
BuilderWatcherCallback,
BuilderWatcherFactory,
} from '../webpack/plugins/builder-watch-plugin';
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 { BuilderWatcherFactory };