Skip to content

Commit 11d6f1a

Browse files
claudiorodriguezevanlucas
authored andcommitted
fs: rename event to eventType in fs.watch listener
The name 'event' for the argument of the listener in fs.watch was confusing considering FSWatcher also had events. This changes the name of the argument to eventType. Fixes: #7504 PR-URL: #7506 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 432cb35 commit 11d6f1a

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

doc/api/fs.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,19 @@ non-UTF-8 encoded Buffers to `fs` functions will not work as expected.
109109
added: v0.5.8
110110
-->
111111

112-
Objects returned from `fs.watch()` are of this type.
112+
Objects returned from [`fs.watch()`][] are of this type.
113+
114+
The `listener` callback provided to `fs.watch()` receives the returned FSWatcher's
115+
`change` events.
116+
117+
The object itself emits these events:
113118

114119
### Event: 'change'
115120
<!-- YAML
116121
added: v0.5.8
117122
-->
118123

119-
* `event` {String} The type of fs change
124+
* `eventType` {String} The type of fs change
120125
* `filename` {String | Buffer} The filename that changed (if relevant/available)
121126

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

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

1446-
The listener callback gets two arguments `(event, filename)`. `event` is either
1452+
The listener callback gets two arguments `(eventType, filename)`. `eventType` is either
14471453
`'rename'` or `'change'`, and `filename` is the name of the file which triggered
14481454
the event.
14491455

1456+
Please note the listener callback is attached to the `'change'` event
1457+
fired by [`fs.FSWatcher`][], but they are not the same thing.
1458+
14501459
### Caveats
14511460

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

15011510
```js
1502-
fs.watch('somedir', (event, filename) => {
1503-
console.log(`event is: ${event}`);
1511+
fs.watch('somedir', (eventType, filename) => {
1512+
console.log(`event type is: ${eventType}`);
15041513
if (filename) {
15051514
console.log(`filename provided: ${filename}`);
15061515
} else {

lib/fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ function FSWatcher() {
13991399
this._handle = new FSEvent();
14001400
this._handle.owner = this;
14011401

1402-
this._handle.onchange = function(status, event, filename) {
1402+
this._handle.onchange = function(status, eventType, filename) {
14031403
if (status < 0) {
14041404
self._handle.close();
14051405
const error = !filename ?
@@ -1409,7 +1409,7 @@ function FSWatcher() {
14091409
error.filename = filename;
14101410
self.emit('error', error);
14111411
} else {
1412-
self.emit('change', event, filename);
1412+
self.emit('change', eventType, filename);
14131413
}
14141414
};
14151415
}

0 commit comments

Comments
 (0)