Browse Source

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
v0.9.1-release
Andreas Madsen 13 years ago
committed by isaacs
parent
commit
a039bad299
  1. 2
      doc/api/fs.markdown
  2. 7
      lib/fs.js
  3. 21
      test/simple/test-fs-watch.js

2
doc/api/fs.markdown

@ -477,7 +477,7 @@ you need to compare `curr.mtime` and `prev.mtime`.
Stop watching for changes on `filename`.
## fs.watch(filename, [options], listener)
## fs.watch(filename, [options], [listener])
Stability: 2 - Unstable. Not available on all platforms.

7
lib/fs.js

@ -773,16 +773,15 @@ fs.watch = function(filename) {
listener = arguments[1];
}
if (!listener) {
throw new Error('watch requires a listener function');
}
if (options.persistent === undefined) options.persistent = true;
watcher = new FSWatcher();
watcher.start(filename, options.persistent);
if (listener) {
watcher.addListener('change', listener);
}
return watcher;
};

21
test/simple/test-fs-watch.js

@ -58,18 +58,10 @@ try { fs.rmdirSync(testsubdir); } catch (e) { }
fs.writeFileSync(filepathOne, 'hello');
assert.throws(
function() {
fs.watch(filepathOne);
},
function(e) {
return e.message === 'watch requires a listener function';
}
);
assert.doesNotThrow(
function() {
var watcher = fs.watch(filepathOne, function(event, filename) {
var watcher = fs.watch(filepathOne)
watcher.on('change', function(event, filename) {
assert.equal('change', event);
if (expectFilePath) {
assert.equal('watch.txt', filename);
@ -91,15 +83,6 @@ process.chdir(testDir);
fs.writeFileSync(filepathTwoAbs, 'howdy');
assert.throws(
function() {
fs.watch(filepathTwo);
},
function(e) {
return e.message === 'watch requires a listener function';
}
);
assert.doesNotThrow(
function() {
var watcher = fs.watch(filepathTwo, function(event, filename) {

Loading…
Cancel
Save