From 6de2173d7c6bd6bb41b571ccbd2c5d9e80131224 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Tue, 17 Nov 2009 22:35:47 +0100 Subject: [PATCH] Add options to process.watchFile() --- doc/api.txt | 7 ++++++- src/node.js | 19 +++++++++++++++++-- src/node_stat.cc | 7 ++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/doc/api.txt b/doc/api.txt index aa6949b87a..7692f4987e 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -124,9 +124,14 @@ Send a signal to a process. +pid+ is the process id and +signal+ is the signal to send; for example, "SIGINT" or "SIGUSR1". See kill(2) for more information. -+process.watchFile(filename, listener)+:: ++process.watchFile(filename, [options,] listener)+:: Watch for changes on +filename+. The callback +listener+ will be called each time the file changes. ++ +The second argument is optional. The +options+ if provided should be an +object containing two members a boolean, +persistent+, and +interval+, a +polling value in milliseconds. The default is +{persistent: true, interval: +0}+. +process.unwatchFile(filename)+:: Stop watching for changes on +filename+. diff --git a/src/node.js b/src/node.js index a471160122..27a63677e8 100644 --- a/src/node.js +++ b/src/node.js @@ -336,14 +336,29 @@ process.addListener("newListener", function (event) { var statWatchers = {}; -process.watchFile = function (filename, listener) { +process.watchFile = function (filename) { var stat; + var options; + var listener; + + if ("object" == typeof arguments[1]) { + options = arguments[1]; + listener = arguments[2]; + } else { + options = {}; + listener = arguments[1]; + } + + if (options.persistent === undefined) options.persistent = true; + if (options.interval === undefined) options.persistent = 0; + + if (filename in statWatchers) { stat = statWatchers[filename]; } else { statWatchers[filename] = new process.Stat(); stat = statWatchers[filename]; - stat.start(filename, true); + stat.start(filename, options.persistent, options.interval); } stat.addListener("change", listener); return stat; diff --git a/src/node_stat.cc b/src/node_stat.cc index 282252fcff..f94e144252 100644 --- a/src/node_stat.cc +++ b/src/node_stat.cc @@ -57,7 +57,12 @@ Handle Stat::Start(const Arguments& args) { assert(handler->path_ == NULL); handler->path_ = strdup(*path); - ev_stat_set(&handler->watcher_, handler->path_, 0.); + ev_tstamp interval = 0.; + if (args[2]->IsInt32()) { + interval = NODE_V8_UNIXTIME(args[2]); + } + + ev_stat_set(&handler->watcher_, handler->path_, interval); ev_stat_start(EV_DEFAULT_UC_ &handler->watcher_); handler->persistent_ = args[1]->IsTrue();