Permalink
Browse files

fs: make fs.watchFile() interval default to 5007

  • Loading branch information...
1 parent f0ce984 commit ef1ffcb717aa16549eb4380badb9709aca3aeb46 @bnoordhuis bnoordhuis committed Jun 21, 2012
Showing with 14 additions and 34 deletions.
  1. +9 −6 lib/fs.js
  2. +5 −25 src/node_stat_watcher.cc
  3. +0 −3 src/node_stat_watcher.h
View
@@ -912,24 +912,27 @@ function inStatWatchers(filename) {
fs.watchFile = function(filename) {
var stat;
- var options;
var listener;
+ var options = {
+ // 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 ('object' == typeof arguments[1]) {
- options = arguments[1];
+ options = util._extend(options, arguments[1]);
listener = arguments[2];
} else {
- options = {};
listener = arguments[1];
}
if (!listener) {
throw new Error('watchFile requires a listener function');
}
- if (options.persistent === undefined) options.persistent = true;
- if (options.interval === undefined) options.interval = 0;
-
if (inStatWatchers(filename)) {
stat = statWatchers[filename];
} else {
@@ -25,11 +25,6 @@
#include <string.h>
#include <stdlib.h>
-// 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, keep behavioral changes to
-// a minimum.
-#define DEFAULT_POLL_INTERVAL 5007
-
namespace node {
using namespace v8;
@@ -76,10 +71,7 @@ void StatWatcher::Callback(uv_fs_poll_t* handle,
Handle<Value> StatWatcher::New(const Arguments& args) {
- if (!args.IsConstructCall()) {
- return FromConstructorTemplate(constructor_template, args);
- }
-
+ assert(args.IsConstructCall());
HandleScope scope;
StatWatcher* s = new StatWatcher();
s->Wrap(args.Holder());
@@ -88,28 +80,16 @@ Handle<Value> StatWatcher::New(const Arguments& args) {
Handle<Value> StatWatcher::Start(const Arguments& args) {
+ assert(args.Length() == 3);
HandleScope scope;
- if (args.Length() < 1 || !args[0]->IsString()) {
- return ThrowException(Exception::TypeError(String::New("Bad arguments")));
- }
-
StatWatcher* wrap = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
String::Utf8Value path(args[0]);
+ const bool persistent = args[1]->BooleanValue();
+ const uint32_t interval = args[2]->Uint32Value();
- uint32_t interval = DEFAULT_POLL_INTERVAL;
- if (args[2]->IsUint32()) {
- interval = args[2]->Uint32Value();
- }
-
+ if (!persistent) uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->watcher_));
uv_fs_poll_start(&wrap->watcher_, Callback, *path, interval);
-
- wrap->persistent_ = args[1]->IsTrue();
-
- if (!wrap->persistent_) {
- uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->watcher_));
- }
-
wrap->Ref();
return Undefined();
@@ -35,7 +35,6 @@ class StatWatcher : ObjectWrap {
static v8::Persistent<v8::FunctionTemplate> constructor_template;
StatWatcher() : ObjectWrap() {
- persistent_ = false;
uv_fs_poll_init(uv_default_loop(), &watcher_);
}
@@ -52,11 +51,9 @@ class StatWatcher : ObjectWrap {
int status,
const uv_statbuf_t* prev,
const uv_statbuf_t* curr);
-
void Stop();
uv_fs_poll_t watcher_;
- bool persistent_;
};
} // namespace node

0 comments on commit ef1ffcb

Please sign in to comment.