-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
fs.watchFile fixes #2093
fs.watchFile fixes #2093
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1305,28 +1305,27 @@ StatWatcher.prototype.stop = function() { | |
|
||
const statWatchers = new Map(); | ||
|
||
fs.watchFile = function(filename) { | ||
fs.watchFile = function(filename, options, listener) { | ||
nullCheck(filename); | ||
filename = pathModule.resolve(filename); | ||
var stat; | ||
var listener; | ||
|
||
var options = { | ||
var defaults = { | ||
// Poll interval in milliseconds. 5007 is what libev used to use. It's | ||
// a little on the slow side but let's stick with it for now to keep | ||
// behavioral changes to a minimum. | ||
interval: 5007, | ||
persistent: true | ||
}; | ||
|
||
if (arguments[1] !== null && typeof arguments[1] === 'object') { | ||
options = util._extend(options, arguments[1]); | ||
listener = arguments[2]; | ||
if (options !== null && typeof options === 'object') { | ||
options = util._extend(defaults, options); | ||
} else { | ||
listener = arguments[1]; | ||
listener = options; | ||
options = defaults; | ||
} | ||
|
||
if (!listener) { | ||
if (typeof listener !== 'function') { | ||
throw new Error('watchFile requires a listener function'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit late, since already merged, but shouldn't this be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never too late :) you're right though, if you wanted to submit a PR I'd review it. |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const assert = require('assert'); | ||
const common = require('../common'); | ||
const fixtures = path.join(__dirname, '..', 'fixtures'); | ||
|
||
// Basic usage tests. | ||
assert.throws(function() { | ||
fs.watchFile('./some-file'); | ||
}, /watchFile requires a listener function/); | ||
|
||
assert.throws(function() { | ||
fs.watchFile('./another-file', {}, 'bad listener'); | ||
}, /watchFile requires a listener function/); | ||
|
||
assert.throws(function() { | ||
fs.watchFile(new Object(), function() {}); | ||
}, /Path must be a string/); | ||
|
||
// Test ENOENT. Should fire once. | ||
const enoentFile = path.join(fixtures, 'empty', 'non-existent-file'); | ||
fs.watchFile(enoentFile, common.mustCall(function(curr, prev) { | ||
fs.unwatchFile(enoentFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, we are not doing inode check to confirm to see if the file really doesn't exist. Meh. Its okay I guess. LGTM 👍 |
||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that it would be better if we can have an example here to show how to differentiate between
ENONET
case and the normal case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, but #2093 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @thefourtheye
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we don't mention the changed versions in the docs I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is a doc that mentions a change in versions:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is an instance of
fs.Stat
, I think that would belong there, rather than in thefs.watchFile
docs. Perhaps this should be done in a separate PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya, it makes sense.