1 |
3a515b92
|
cagy
|
# watchpack
|
2 |
|
|
|
3 |
|
|
Wrapper library for directory and file watching.
|
4 |
|
|
|
5 |
|
|
[](https://travis-ci.org/webpack/watchpack) [](https://ci.appveyor.com/project/sokra/watchpack/branch/master) [![Test coverage][coveralls-image]][coveralls-url]
|
6 |
|
|
|
7 |
|
|
## Concept
|
8 |
|
|
|
9 |
|
|
watchpack high level API doesn't map directly to watchers. Instead a three level architecture ensures that for each directory only a single watcher exists.
|
10 |
|
|
|
11 |
|
|
* The high level API requests `DirectoryWatchers` from a `WatcherManager`, which ensures that only a single `DirectoryWatcher` per directory is created.
|
12 |
|
|
* A user-faced `Watcher` can be obtained from a `DirectoryWatcher` and provides a filtered view on the `DirectoryWatcher`.
|
13 |
|
|
* Reference-counting is used on the `DirectoryWatcher` and `Watcher` to decide when to close them.
|
14 |
|
|
* The real watchers (currently chokidar) are created by the `DirectoryWatcher`.
|
15 |
|
|
* Files are never watched directly. This should keep the watcher count low.
|
16 |
|
|
* Watching can be started in the past. This way watching can start after file reading.
|
17 |
|
|
* Symlinks are not followed, instead the symlink is watched.
|
18 |
|
|
|
19 |
|
|
## API
|
20 |
|
|
|
21 |
|
|
``` javascript
|
22 |
|
|
var Watchpack = require("watchpack");
|
23 |
|
|
|
24 |
|
|
var wp = new Watchpack({
|
25 |
|
|
// options:
|
26 |
|
|
aggregateTimeout: 1000
|
27 |
|
|
// fire "aggregated" event when after a change for 1000ms no additional change occurred
|
28 |
|
|
// aggregated defaults to undefined, which doesn't fire an "aggregated" event
|
29 |
|
|
|
30 |
|
|
poll: true
|
31 |
|
|
// poll: true - use polling with the default interval
|
32 |
|
|
// poll: 10000 - use polling with an interval of 10s
|
33 |
|
|
// poll defaults to undefined, which prefer native watching methods
|
34 |
|
|
// Note: enable polling when watching on a network path
|
35 |
|
|
|
36 |
|
|
ignored: /node_modules/,
|
37 |
|
|
// anymatch-compatible definition of files/paths to be ignored
|
38 |
|
|
// see https://github.com/paulmillr/chokidar#path-filtering
|
39 |
|
|
});
|
40 |
|
|
|
41 |
|
|
// Watchpack.prototype.watch(string[] files, string[] directories, [number startTime])
|
42 |
|
|
wp.watch(listOfFiles, listOfDirectories, Date.now() - 10000);
|
43 |
|
|
// starts watching these files and directories
|
44 |
|
|
// calling this again will override the files and directories
|
45 |
|
|
|
46 |
|
|
wp.on("change", function(filePath, mtime) {
|
47 |
|
|
// filePath: the changed file
|
48 |
|
|
// mtime: last modified time for the changed file
|
49 |
|
|
});
|
50 |
|
|
|
51 |
|
|
wp.on("aggregated", function(changes) {
|
52 |
|
|
// changes: an array of all changed files
|
53 |
|
|
});
|
54 |
|
|
|
55 |
|
|
// Watchpack.prototype.pause()
|
56 |
|
|
wp.pause();
|
57 |
|
|
// stops emitting events, but keeps watchers open
|
58 |
|
|
// next "watch" call can reuse the watchers
|
59 |
|
|
|
60 |
|
|
// Watchpack.prototype.close()
|
61 |
|
|
wp.close();
|
62 |
|
|
// stops emitting events and closes all watchers
|
63 |
|
|
|
64 |
|
|
// Watchpack.prototype.getTimes()
|
65 |
|
|
var fileTimes = wp.getTimes();
|
66 |
|
|
// returns an object with all know change times for files
|
67 |
|
|
// this include timestamps from files not directly watched
|
68 |
|
|
// key: absolute path, value: timestamp as number
|
69 |
|
|
```
|
70 |
|
|
|
71 |
|
|
[coveralls-url]: https://coveralls.io/r/webpack/watchpack/
|
72 |
|
|
[coveralls-image]: https://img.shields.io/coveralls/webpack/watchpack.svg
|