DirectoryFsWatcher
Summary
Defines a directory watcher which is a wrapper around Node.js fs.watch.
fs.watch only emit "change" and "rename" events, nothing more. This class intends to make it more user friendly by emitting more understandable events like "create" or "delete".
It analyzes only "rename" events, "change" events are not constant between Linux and Windows systems. On both Linux and Windows systems : When a file is added a "rename" event is emitted followed by a "change" event When a file is renamed a "rename" event is emitted twice (one for the removal and one for the addition) When a file is removed a "rename" event is emitted
To work through this behaviour, a snapshot of the directory is made each time a "rename" event is fired. Then new resources inside the directory are compared to the snapshot to find out which resource has been added or removed.
If a "rename" event is emitted while the directory is being analyzed, directory will be analyzed again making every "rename" event count.
Constructor
DirectoryFsWatcher
Syntax
DirectoryFsWatcher
-
directoryPath
-
[options]
Summary
Item Index
Methods
Properties
Methods
awaitWriteFinish
Syntax
awaitWriteFinish
-
resourcePath
-
[lastStat]
Summary
Waits for the resource to be fully written.
Parameters:
-
resourcePath
StringThe absolute path of the resource to wait for
-
[lastStat]
fs.Stats optionalLast information about the resource
close
Syntax
close
()
Summary
Stops watching the directory.
interpretRename
Syntax
interpretRename
()
private
async
Summary
Interprets a "rename" event.
If "rename" event corresponds to an addition, a "create" event will be fired. If "rename" event corresponds to a removal, a "delete" event will be fired.
watch
Syntax
watch
-
callback
Summary
Watches the directory.
Parameters:
-
callback
FunctionThe function to call when its starts listening to changes
- Error An error if something went wrong
Properties
fsWatcher
Syntax
fsWatcher
fs.FSWatcher
Summary
The Node.js directory watcher.
pendingEventsCounter
Syntax
pendingEventsCounter
Number
Summary
The number of "rename" events which haven't been treated yet.
snapshot
Syntax
snapshot
Object
Summary
The resources inside the directory with the resource path as the key and true as the value.
The snapshot represents the current content of the directory and will be updated after each "rename" event.
Events
create
Syntax
create
Summary
Fired when a new resource (file or directory) has been added to the directory.
Event Payload:
-
Path
Stringof the added resource
delete
Syntax
delete
Summary
Fired when a resource (file or directory) has been deleted from the directory.
Event Payload:
-
Path
Stringof the resource before it has been removed