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. 9
      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`. 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.

9
lib/fs.js

@ -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;
}; };

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

@ -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…
Cancel
Save