Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add options to process.watchFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Nov 17, 2009
1 parent 027829d commit 6de2173
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
7 changes: 6 additions & 1 deletion doc/api.txt
Expand Up @@ -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+.
Expand Down
19 changes: 17 additions & 2 deletions src/node.js
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/node_stat.cc
Expand Up @@ -57,7 +57,12 @@ Handle<Value> 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();
Expand Down

0 comments on commit 6de2173

Please sign in to comment.