Skip to content

doc: clarify 'change' in fs.watch and FSWatcher #7506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,19 @@ non-UTF-8 encoded Buffers to `fs` functions will not work as expected.
added: v0.5.8
-->

Objects returned from `fs.watch()` are of this type.
Objects returned from [`fs.watch()`][] are of this type.

The `listener` callback provided to `fs.watch()` receives the returned FSWatcher's
`change` events.

The object itself emits these events:

### Event: 'change'
<!-- YAML
added: v0.5.8
-->

* `event` {String} The type of fs change
* `eventType` {String} The type of fs change
* `filename` {String | Buffer} The filename that changed (if relevant/available)

Emitted when something changes in a watched directory or file.
Expand All @@ -128,7 +133,8 @@ support. If `filename` is provided, it will be provided as a `Buffer` if
`filename` will be a string.

```js
fs.watch('./tmp', {encoding: 'buffer'}, (event, filename) => {
// Example when handled through fs.watch listener
fs.watch('./tmp', {encoding: 'buffer'}, (eventType, filename) => {
if (filename)
console.log(filename);
// Prints: <Buffer ...>
Expand Down Expand Up @@ -1443,10 +1449,13 @@ directory. The returned object is a [`fs.FSWatcher`][].
The second argument is optional. If `options` is provided as a string, it
specifies the `encoding`. Otherwise `options` should be passed as an object.

The listener callback gets two arguments `(event, filename)`. `event` is either
The listener callback gets two arguments `(eventType, filename)`. `eventType` is either
`'rename'` or `'change'`, and `filename` is the name of the file which triggered
the event.

Please note the listener callback is attached to the `'change'` event
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the actual confusion in the original issue, is because of the previous paragraph. IINW, OP misunderstood the type of event (rename and change) as the actual events emitted. Perhaps we can change/explain the parameter event.

Thoughts @Trott?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thefourtheye I could change the name in both the code and docs to eventType or something like that - it's just a change of identifier name so it wouldn't be intrusive/breaking

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think changing event to eventType would be a significant improvement.

fired by [`fs.FSWatcher`][], but they are not the same thing.

### Caveats

<!--type=misc-->
Expand Down Expand Up @@ -1499,8 +1508,8 @@ be provided. Therefore, don't assume that `filename` argument is always
provided in the callback, and have some fallback logic if it is null.

```js
fs.watch('somedir', (event, filename) => {
console.log(`event is: ${event}`);
fs.watch('somedir', (eventType, filename) => {
console.log(`event type is: ${eventType}`);
if (filename) {
console.log(`filename provided: ${filename}`);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ function FSWatcher() {
this._handle = new FSEvent();
this._handle.owner = this;

this._handle.onchange = function(status, event, filename) {
this._handle.onchange = function(status, eventType, filename) {
if (status < 0) {
self._handle.close();
const error = !filename ?
Expand All @@ -1409,7 +1409,7 @@ function FSWatcher() {
error.filename = filename;
self.emit('error', error);
} else {
self.emit('change', event, filename);
self.emit('change', eventType, filename);
}
};
}
Expand Down