|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var Watchpack = require("watchpack"); |
| 5 | +"use strict"; |
6 | 6 |
|
7 | | -function NodeWatchFileSystem(inputFileSystem) { |
8 | | - this.inputFileSystem = inputFileSystem; |
9 | | - this.watcherOptions = { |
10 | | - aggregateTimeout: 0 |
11 | | - }; |
12 | | - this.watcher = new Watchpack(this.watcherOptions); |
13 | | -} |
| 7 | +const Watchpack = require("watchpack"); |
14 | 8 |
|
15 | | -module.exports = NodeWatchFileSystem; |
| 9 | +class NodeWatchFileSystem { |
| 10 | + constructor(inputFileSystem) { |
| 11 | + this.inputFileSystem = inputFileSystem; |
| 12 | + this.watcherOptions = { |
| 13 | + aggregateTimeout: 0 |
| 14 | + }; |
| 15 | + this.watcher = new Watchpack(this.watcherOptions); |
| 16 | + } |
16 | 17 |
|
17 | | -NodeWatchFileSystem.prototype.watch = function watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) { |
18 | | - if(!Array.isArray(files)) |
19 | | - throw new Error("Invalid arguments: 'files'"); |
20 | | - if(!Array.isArray(dirs)) |
21 | | - throw new Error("Invalid arguments: 'dirs'"); |
22 | | - if(!Array.isArray(missing)) |
23 | | - throw new Error("Invalid arguments: 'missing'"); |
24 | | - if(typeof callback !== "function") |
25 | | - throw new Error("Invalid arguments: 'callback'"); |
26 | | - if(typeof startTime !== "number" && startTime) |
27 | | - throw new Error("Invalid arguments: 'startTime'"); |
28 | | - if(typeof options !== "object") |
29 | | - throw new Error("Invalid arguments: 'options'"); |
30 | | - if(typeof callbackUndelayed !== "function" && callbackUndelayed) |
31 | | - throw new Error("Invalid arguments: 'callbackUndelayed'"); |
32 | | - var oldWatcher = this.watcher; |
33 | | - this.watcher = new Watchpack(options); |
| 18 | + watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) { |
| 19 | + if(!Array.isArray(files)) |
| 20 | + throw new Error("Invalid arguments: 'files'"); |
| 21 | + if(!Array.isArray(dirs)) |
| 22 | + throw new Error("Invalid arguments: 'dirs'"); |
| 23 | + if(!Array.isArray(missing)) |
| 24 | + throw new Error("Invalid arguments: 'missing'"); |
| 25 | + if(typeof callback !== "function") |
| 26 | + throw new Error("Invalid arguments: 'callback'"); |
| 27 | + if(typeof startTime !== "number" && startTime) |
| 28 | + throw new Error("Invalid arguments: 'startTime'"); |
| 29 | + if(typeof options !== "object") |
| 30 | + throw new Error("Invalid arguments: 'options'"); |
| 31 | + if(typeof callbackUndelayed !== "function" && callbackUndelayed) |
| 32 | + throw new Error("Invalid arguments: 'callbackUndelayed'"); |
| 33 | + const oldWatcher = this.watcher; |
| 34 | + this.watcher = new Watchpack(options); |
34 | 35 |
|
35 | | - if(callbackUndelayed) |
36 | | - this.watcher.once("change", callbackUndelayed); |
| 36 | + if(callbackUndelayed) |
| 37 | + this.watcher.once("change", callbackUndelayed); |
37 | 38 |
|
38 | | - this.watcher.once("aggregated", function(changes) { |
39 | | - if(this.inputFileSystem && this.inputFileSystem.purge) { |
40 | | - this.inputFileSystem.purge(changes); |
41 | | - } |
42 | | - var times = this.watcher.getTimes(); |
43 | | - callback(null, changes.filter(function(file) { |
44 | | - return files.indexOf(file) >= 0; |
45 | | - }).sort(), changes.filter(function(file) { |
46 | | - return dirs.indexOf(file) >= 0; |
47 | | - }).sort(), changes.filter(function(file) { |
48 | | - return missing.indexOf(file) >= 0; |
49 | | - }).sort(), times, times); |
50 | | - }.bind(this)); |
| 39 | + this.watcher.once("aggregated", function(changes) { |
| 40 | + if(this.inputFileSystem && this.inputFileSystem.purge) { |
| 41 | + this.inputFileSystem.purge(changes); |
| 42 | + } |
| 43 | + const times = this.watcher.getTimes(); |
| 44 | + callback(null, |
| 45 | + changes.filter(file => files.indexOf(file) >= 0).sort(), |
| 46 | + changes.filter(file => dirs.indexOf(file) >= 0).sort(), |
| 47 | + changes.filter(file => missing.indexOf(file) >= 0).sort(), times, times); |
| 48 | + }.bind(this)); |
51 | 49 |
|
52 | | - this.watcher.watch(files.concat(missing), dirs, startTime); |
| 50 | + this.watcher.watch(files.concat(missing), dirs, startTime); |
53 | 51 |
|
54 | | - if(oldWatcher) { |
55 | | - oldWatcher.close(); |
| 52 | + if(oldWatcher) { |
| 53 | + oldWatcher.close(); |
| 54 | + } |
| 55 | + return { |
| 56 | + close: function() { |
| 57 | + this.watcher.close(); |
| 58 | + }.bind(this), |
| 59 | + pause: function() { |
| 60 | + this.watcher.pause(); |
| 61 | + }.bind(this) |
| 62 | + }; |
56 | 63 | } |
57 | | - return { |
58 | | - close: function() { |
59 | | - this.watcher.close(); |
60 | | - }.bind(this), |
61 | | - pause: function() { |
62 | | - this.watcher.pause(); |
63 | | - }.bind(this) |
64 | | - }; |
65 | | -}; |
| 64 | +} |
| 65 | + |
| 66 | +module.exports = NodeWatchFileSystem; |
0 commit comments