-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWatchesFile.js
54 lines (47 loc) · 1.05 KB
/
WatchesFile.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
42
43
44
45
46
47
48
49
50
51
52
53
54
const Watcher = require('./Watcher');
/**
* A mixin that provides a file watcher for this.fileName which triggers a method `onChange` on the class, that needs to be implemented.
*
* @template T
* @param {T} Base
* @returns {T} An anonymous class that extends Base
*/
const WatchesFile = Base => class extends Base {
/**
* @private
*/
watchFile() {
this.stopWatching();
this.watcher = new Watcher(this.fileName);
this.watcher.on('change', this.onChange.bind(this));
this.watcher.on('rename', this.onRename.bind(this));
}
/**
* @private
*/
stopWatching() {
if (this.watcher) {
this.watcher.close();
this.watcher = null;
}
}
/**
* @api
* @returns {boolean}
*/
open() {
if (super.open()) {
this.watchFile();
return true;
}
return false;
}
/**
* @api
*/
close() {
this.stopWatching();
super.close();
}
};
module.exports = WatchesFile;