1
Fork 0

instance name adjustments

This commit is contained in:
Conduitry 2018-04-03 21:19:48 -04:00
parent 593d3e0eba
commit 64612f7107
2 changed files with 22 additions and 22 deletions

View file

@ -15,19 +15,19 @@
## Methods
### `cheapWatch.init()`
### `init()`
Initialize the watcher, traverse the directory to find the initial files and directories, and set up watchers to look for changes.
This returns a `Promise` that resolves once the initial contents of the directory have been traversed and all of the watchers have been set up.
### `cheapWatch.close()`
### `close()`
Close all `FSWatcher` instances, and stop watching for file changes.
## Properties
### `cheapWatch.paths`
### `paths`
A `Map` of the watched files and directories. Each key is a relative path from the `CheapWatch`'s `dir`, and each value is a `Stats` object for the file or directory. Paths are always separated by forward slashes, regardless of platform. This `Map` is kept up to date as files are changed on disk.
@ -50,16 +50,16 @@ A `-` event is emitted with an object containing a `path` string and a `stats` o
```javascript
import CheapWatch from 'cheap-watch';
const watcher = new CheapWatch({ dir, /* ... */ });
const watch = new CheapWatch({ dir, /* ... */ });
await watcher.init();
await watch.init();
for (const [path, stats] of watcher.paths) {
for (const [path, stats] of watch.paths) {
/* ... */
}
watcher.on('+', ({ path, stats }) => { /* ... */ });
watcher.on('-', ({ path, stats }) => { /* ... */ });
watch.on('+', ({ path, stats }) => { /* ... */ });
watch.on('-', ({ path, stats }) => { /* ... */ });
```
## License

28
test.js
View file

@ -20,10 +20,10 @@ const rmdir =
const sleep = (ms = 100) => new Promise(res => setTimeout(res, ms));
function getEvents(watcher) {
function getEvents(watch) {
const events = new Set();
for (const event of ['+', '-']) {
watcher.on(event, ({ path, stats }) => {
watch.on(event, ({ path, stats }) => {
events.add(event + (stats.isFile() ? 'f ' : 'd ') + path);
});
}
@ -38,17 +38,17 @@ function getEvents(watcher) {
console.log('running tests ...');
const watcher = new CheapWatch({ dir: process.cwd() });
const events = getEvents(watcher);
const watch = new CheapWatch({ dir: process.cwd() });
const events = getEvents(watch);
await writeFile('foo', '');
await mkdir('bar');
await writeFile('bar/baz', '');
await watcher.init();
assert.equal(watcher.paths.size, 3);
assert.ok(watcher.paths.get('foo').isFile());
assert.ok(watcher.paths.get('bar').isDirectory());
assert.ok(watcher.paths.get('bar/baz').isFile());
await watch.init();
assert.equal(watch.paths.size, 3);
assert.ok(watch.paths.get('foo').isFile());
assert.ok(watch.paths.get('bar').isDirectory());
assert.ok(watch.paths.get('bar/baz').isFile());
await writeFile('foo', 'foo');
await sleep();
@ -79,21 +79,21 @@ function getEvents(watcher) {
assert.ok(events.has('+f bar'));
events.clear();
watcher.close();
watch.close();
await writeFile('foo', '');
await sleep();
assert.equal(events.size, 0);
const watcher2 = new CheapWatch({
const watch2 = new CheapWatch({
dir: process.cwd(),
filter: ({ path, stats }) =>
(stats.isFile() && !path.includes('skip-file')) ||
(stats.isDirectory() && !path.includes('skip-directory')),
});
const events2 = getEvents(watcher2);
const events2 = getEvents(watch2);
await watcher2.init();
await watch2.init();
await writeFile('skip-file', '');
await sleep();
@ -112,7 +112,7 @@ function getEvents(watcher) {
await sleep();
assert.equal(events2.size, 0);
watcher2.close();
watch2.close();
console.log('tests successful!');