-
Notifications
You must be signed in to change notification settings - Fork 32
/
Watcher.js
41 lines (34 loc) · 962 Bytes
/
Watcher.js
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
"use strict";
const chokidar = require("chokidar");
const debounce = require("debounce");
class Watcher {
constructor(emitter) {
this.emitter = emitter;
this.files = new Map();
this.watch = chokidar.watch();
this.watch.on("change", debounce(this.handleChange.bind(this), 150));
}
add(entry, dependencies) {
if (!hasNullByte(entry)) {
const filteredDependencies = dependencies.filter(
path => !hasNullByte(path)
);
this.files.set(entry, filteredDependencies);
this.watch.add([entry, ...filteredDependencies]);
}
}
handleChange(path) {
for (const [entry, dependencies] of this.files.entries()) {
if (entry === path || dependencies.includes(path)) {
return this.refreshFile(entry);
}
}
}
refreshFile(path) {
this.emitter._fileList.changeFile(path, true);
}
}
function hasNullByte(string) {
return string.includes("\u0000");
}
module.exports = Watcher;