Skip to content

Commit ef1ffcb

Browse files
committed
fs: make fs.watchFile() interval default to 5007
1 parent f0ce984 commit ef1ffcb

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

lib/fs.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -912,24 +912,27 @@ function inStatWatchers(filename) {
912912

913913
fs.watchFile = function(filename) {
914914
var stat;
915-
var options;
916915
var listener;
917916

917+
var options = {
918+
// Poll interval in milliseconds. 5007 is what libev used to use. It's
919+
// a little on the slow side but let's stick with it for now to keep
920+
// behavioral changes to a minimum.
921+
interval: 5007,
922+
persistent: true,
923+
};
924+
918925
if ('object' == typeof arguments[1]) {
919-
options = arguments[1];
926+
options = util._extend(options, arguments[1]);
920927
listener = arguments[2];
921928
} else {
922-
options = {};
923929
listener = arguments[1];
924930
}
925931

926932
if (!listener) {
927933
throw new Error('watchFile requires a listener function');
928934
}
929935

930-
if (options.persistent === undefined) options.persistent = true;
931-
if (options.interval === undefined) options.interval = 0;
932-
933936
if (inStatWatchers(filename)) {
934937
stat = statWatchers[filename];
935938
} else {

src/node_stat_watcher.cc

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
#include <string.h>
2626
#include <stdlib.h>
2727

28-
// Poll interval in milliseconds. 5007 is what libev used to use. It's a little
29-
// on the slow side but let's stick with it for now, keep behavioral changes to
30-
// a minimum.
31-
#define DEFAULT_POLL_INTERVAL 5007
32-
3328
namespace node {
3429

3530
using namespace v8;
@@ -76,10 +71,7 @@ void StatWatcher::Callback(uv_fs_poll_t* handle,
7671

7772

7873
Handle<Value> StatWatcher::New(const Arguments& args) {
79-
if (!args.IsConstructCall()) {
80-
return FromConstructorTemplate(constructor_template, args);
81-
}
82-
74+
assert(args.IsConstructCall());
8375
HandleScope scope;
8476
StatWatcher* s = new StatWatcher();
8577
s->Wrap(args.Holder());
@@ -88,28 +80,16 @@ Handle<Value> StatWatcher::New(const Arguments& args) {
8880

8981

9082
Handle<Value> StatWatcher::Start(const Arguments& args) {
83+
assert(args.Length() == 3);
9184
HandleScope scope;
9285

93-
if (args.Length() < 1 || !args[0]->IsString()) {
94-
return ThrowException(Exception::TypeError(String::New("Bad arguments")));
95-
}
96-
9786
StatWatcher* wrap = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
9887
String::Utf8Value path(args[0]);
88+
const bool persistent = args[1]->BooleanValue();
89+
const uint32_t interval = args[2]->Uint32Value();
9990

100-
uint32_t interval = DEFAULT_POLL_INTERVAL;
101-
if (args[2]->IsUint32()) {
102-
interval = args[2]->Uint32Value();
103-
}
104-
91+
if (!persistent) uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->watcher_));
10592
uv_fs_poll_start(&wrap->watcher_, Callback, *path, interval);
106-
107-
wrap->persistent_ = args[1]->IsTrue();
108-
109-
if (!wrap->persistent_) {
110-
uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->watcher_));
111-
}
112-
11393
wrap->Ref();
11494

11595
return Undefined();

src/node_stat_watcher.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class StatWatcher : ObjectWrap {
3535
static v8::Persistent<v8::FunctionTemplate> constructor_template;
3636

3737
StatWatcher() : ObjectWrap() {
38-
persistent_ = false;
3938
uv_fs_poll_init(uv_default_loop(), &watcher_);
4039
}
4140

@@ -52,11 +51,9 @@ class StatWatcher : ObjectWrap {
5251
int status,
5352
const uv_statbuf_t* prev,
5453
const uv_statbuf_t* curr);
55-
5654
void Stop();
5755

5856
uv_fs_poll_t watcher_;
59-
bool persistent_;
6057
};
6158

6259
} // namespace node

0 commit comments

Comments
 (0)