fs.watch should not require a listener arguments
Since fs.watch returns an event emitter where the change event is exactly the same as the listener callback, the argument should be required
This commit is contained in:
parent
18b94ea838
commit
a039bad299
@ -477,7 +477,7 @@ you need to compare `curr.mtime` and `prev.mtime`.
|
|||||||
|
|
||||||
Stop watching for changes on `filename`.
|
Stop watching for changes on `filename`.
|
||||||
|
|
||||||
## fs.watch(filename, [options], listener)
|
## fs.watch(filename, [options], [listener])
|
||||||
|
|
||||||
Stability: 2 - Unstable. Not available on all platforms.
|
Stability: 2 - Unstable. Not available on all platforms.
|
||||||
|
|
||||||
|
@ -773,16 +773,15 @@ fs.watch = function(filename) {
|
|||||||
listener = arguments[1];
|
listener = arguments[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!listener) {
|
|
||||||
throw new Error('watch requires a listener function');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.persistent === undefined) options.persistent = true;
|
if (options.persistent === undefined) options.persistent = true;
|
||||||
|
|
||||||
watcher = new FSWatcher();
|
watcher = new FSWatcher();
|
||||||
watcher.start(filename, options.persistent);
|
watcher.start(filename, options.persistent);
|
||||||
|
|
||||||
watcher.addListener('change', listener);
|
if (listener) {
|
||||||
|
watcher.addListener('change', listener);
|
||||||
|
}
|
||||||
|
|
||||||
return watcher;
|
return watcher;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -58,18 +58,10 @@ try { fs.rmdirSync(testsubdir); } catch (e) { }
|
|||||||
|
|
||||||
fs.writeFileSync(filepathOne, 'hello');
|
fs.writeFileSync(filepathOne, 'hello');
|
||||||
|
|
||||||
assert.throws(
|
|
||||||
function() {
|
|
||||||
fs.watch(filepathOne);
|
|
||||||
},
|
|
||||||
function(e) {
|
|
||||||
return e.message === 'watch requires a listener function';
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.doesNotThrow(
|
assert.doesNotThrow(
|
||||||
function() {
|
function() {
|
||||||
var watcher = fs.watch(filepathOne, function(event, filename) {
|
var watcher = fs.watch(filepathOne)
|
||||||
|
watcher.on('change', function(event, filename) {
|
||||||
assert.equal('change', event);
|
assert.equal('change', event);
|
||||||
if (expectFilePath) {
|
if (expectFilePath) {
|
||||||
assert.equal('watch.txt', filename);
|
assert.equal('watch.txt', filename);
|
||||||
@ -91,15 +83,6 @@ process.chdir(testDir);
|
|||||||
|
|
||||||
fs.writeFileSync(filepathTwoAbs, 'howdy');
|
fs.writeFileSync(filepathTwoAbs, 'howdy');
|
||||||
|
|
||||||
assert.throws(
|
|
||||||
function() {
|
|
||||||
fs.watch(filepathTwo);
|
|
||||||
},
|
|
||||||
function(e) {
|
|
||||||
return e.message === 'watch requires a listener function';
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
assert.doesNotThrow(
|
assert.doesNotThrow(
|
||||||
function() {
|
function() {
|
||||||
var watcher = fs.watch(filepathTwo, function(event, filename) {
|
var watcher = fs.watch(filepathTwo, function(event, filename) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user