cheap-watch
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -0,1 +1,8 @@ | ||
# v0.3.0 | ||
- Rename `.files` to `.paths`, as it contains directories, not just files | ||
- Include an additional `isNew` boolean in `+` events, indicating whether this is a new or an updated file/directory | ||
- No longer call the `filter` function on the root watched directory | ||
- No longer include the root watched directory under the `''` in the `Map`, nor emit `+` events for it | ||
# v0.2.2 | ||
@@ -2,0 +9,0 @@ |
@@ -44,3 +44,3 @@ import EventEmitter from 'events'; | ||
// paths of all files/dirs -> stats | ||
this.files = new Map(); | ||
this.paths = new Map(); | ||
// paths of files with pending debounced events -> setTimeout timer ids | ||
@@ -86,6 +86,8 @@ this[_timeouts] = new Map(); | ||
const stats = await statAsync(full); | ||
if (this.filter && !await this.filter({ path, stats })) { | ||
return; | ||
if (path) { | ||
if (this.filter && !await this.filter({ path, stats })) { | ||
return; | ||
} | ||
this.paths.set(path, stats); | ||
} | ||
this.files.set(path, stats); | ||
if (stats.isDirectory()) { | ||
@@ -134,4 +136,7 @@ if (this.watch) { | ||
} | ||
this.files.set(path, stats); | ||
this.emit('+', { path, stats }); | ||
const isNew = !this.paths.has(path); | ||
this.paths.set(path, stats); | ||
if (path) { | ||
this.emit('+', { path, stats, isNew }); | ||
} | ||
if (stats.isDirectory() && !this[_watchers].has(path)) { | ||
@@ -141,5 +146,5 @@ // note the new directory | ||
await this[_recurse](full); | ||
for (const [newPath, stats] of this.files.entries()) { | ||
for (const [newPath, stats] of this.paths.entries()) { | ||
if (newPath.startsWith(path + '/')) { | ||
this.emit('+', { path: newPath, stats }); | ||
this.emit('+', { path: newPath, stats, isNew: true }); | ||
} | ||
@@ -150,6 +155,6 @@ } | ||
// check whether this is a deleted file/dir or just some FSWatcher artifact | ||
if (this.files.has(path)) { | ||
if (this.paths.has(path)) { | ||
// note the deleted file/dir | ||
const stats = this.files.get(path); | ||
this.files.delete(path); | ||
const stats = this.paths.get(path); | ||
this.paths.delete(path); | ||
this.emit('-', { path, stats }); | ||
@@ -164,6 +169,6 @@ if (this[_watchers].has(path)) { | ||
} | ||
for (const old of this.files.keys()) { | ||
for (const old of this.paths.keys()) { | ||
if (old.startsWith(path + '/')) { | ||
const stats = this.files.get(old); | ||
this.files.delete(old); | ||
const stats = this.paths.get(old); | ||
this.paths.delete(old); | ||
this.emit('-', { path: old, stats }); | ||
@@ -170,0 +175,0 @@ } |
@@ -46,3 +46,3 @@ 'use strict'; | ||
// paths of all files/dirs -> stats | ||
this.files = new Map(); | ||
this.paths = new Map(); | ||
// paths of files with pending debounced events -> setTimeout timer ids | ||
@@ -88,6 +88,8 @@ this[_timeouts] = new Map(); | ||
const stats = await statAsync(full); | ||
if (this.filter && !await this.filter({ path, stats })) { | ||
return; | ||
if (path) { | ||
if (this.filter && !await this.filter({ path, stats })) { | ||
return; | ||
} | ||
this.paths.set(path, stats); | ||
} | ||
this.files.set(path, stats); | ||
if (stats.isDirectory()) { | ||
@@ -136,4 +138,7 @@ if (this.watch) { | ||
} | ||
this.files.set(path, stats); | ||
this.emit('+', { path, stats }); | ||
const isNew = !this.paths.has(path); | ||
this.paths.set(path, stats); | ||
if (path) { | ||
this.emit('+', { path, stats, isNew }); | ||
} | ||
if (stats.isDirectory() && !this[_watchers].has(path)) { | ||
@@ -143,5 +148,5 @@ // note the new directory | ||
await this[_recurse](full); | ||
for (const [newPath, stats] of this.files.entries()) { | ||
for (const [newPath, stats] of this.paths.entries()) { | ||
if (newPath.startsWith(path + '/')) { | ||
this.emit('+', { path: newPath, stats }); | ||
this.emit('+', { path: newPath, stats, isNew: true }); | ||
} | ||
@@ -152,6 +157,6 @@ } | ||
// check whether this is a deleted file/dir or just some FSWatcher artifact | ||
if (this.files.has(path)) { | ||
if (this.paths.has(path)) { | ||
// note the deleted file/dir | ||
const stats = this.files.get(path); | ||
this.files.delete(path); | ||
const stats = this.paths.get(path); | ||
this.paths.delete(path); | ||
this.emit('-', { path, stats }); | ||
@@ -166,6 +171,6 @@ if (this[_watchers].has(path)) { | ||
} | ||
for (const old of this.files.keys()) { | ||
for (const old of this.paths.keys()) { | ||
if (old.startsWith(path + '/')) { | ||
const stats = this.files.get(old); | ||
this.files.delete(old); | ||
const stats = this.paths.get(old); | ||
this.paths.delete(old); | ||
this.emit('-', { path: old, stats }); | ||
@@ -172,0 +177,0 @@ } |
{ | ||
"name": "cheap-watch", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "If it works, why use something else?", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -18,3 +18,3 @@ # Cheap Watch: If it works, why use something else? | ||
### `cheapWatch.init()` | ||
### `init()` | ||
@@ -25,3 +25,3 @@ Initialize the watcher, traverse the directory to find the initial files and directories, and set up watchers to look for changes. | ||
### `cheapWatch.close()` | ||
### `close()` | ||
@@ -32,3 +32,3 @@ Close all `FSWatcher` instances, and stop watching for file changes. | ||
### `cheapWatch.files` | ||
### `paths` | ||
@@ -43,9 +43,9 @@ 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. | ||
### `+` `{ path, stats }` | ||
### `+` `{ path, stats, isNew }` | ||
A `+` event is emitted with an object containing a `path` string and a `stats` object whenever a watched file or directory is created or updated. | ||
A `+` event is emitted whenever a watched file or directory is created or updated. It's emitted with an object containing a `path` string, a `stats` object, and an `isNew` boolean which will be `true` for newly created files and directories and `false` for updated ones. | ||
### `-` `{ path, stats }` | ||
A `-` event is emitted with an object containing a `path` string and a `stats` object whenever a watched file or directory is deleted. `stats` will be the most recent `Stats` collected for the file or directory before it was deleted. | ||
A `-` event is emitted whenever a watched file or directory is deleted. It's emitted with an object containing a `path` string and a `stats` object. `stats` will be the most recent `Stats` collected for the file or directory before it was deleted. | ||
@@ -57,12 +57,12 @@ ## Usage | ||
const watcher = new CheapWatch({ dir, /* ... */ }); | ||
const watch = new CheapWatch({ dir, /* ... */ }); | ||
await watcher.init(); | ||
await watch.init(); | ||
for (const [path, stats] of watcher.files) { | ||
for (const [path, stats] of watch.paths) { | ||
/* ... */ | ||
} | ||
watcher.on('+', ({ path, stats }) => { /* ... */ }); | ||
watcher.on('-', ({ path, stats }) => { /* ... */ }); | ||
watch.on('+', ({ path, stats }) => { /* ... */ }); | ||
watch.on('-', ({ path, stats }) => { /* ... */ }); | ||
``` | ||
@@ -69,0 +69,0 @@ |
Sorry, the diff of this file is not supported yet
28622
341