Skip to content

Commit

Permalink
Allow for closing of a watch
Browse files Browse the repository at this point in the history
The previous version had no return value.  Now the function returns its underlying listener so that it can be closed and future changes to the watched file will not trigger events.
  • Loading branch information
jvonmitchell committed Jul 3, 2014
1 parent e386751 commit 22e5936
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/watch.js
Expand Up @@ -166,7 +166,8 @@ var catchException = function() {};
*/
var handleOptions = function(origin, defaultOptions) {
return function() {
var args = [].slice.call(arguments);
var args = [].slice.call(arguments),
retrs=[];
if (Object.prototype.toString.call(args[1]) === '[object Function]') {
args[2] = args[1];
}
Expand All @@ -177,8 +178,9 @@ var handleOptions = function(origin, defaultOptions) {
args[1] = mixin(defaultOptions, args[1]);
//handle multiple files.
args[0].forEach(function(path) {
origin.apply(null, [path].concat(args.slice(1)));
});
retrs.push(origin.apply(null, [path].concat(args.slice(1))));
});
return retrs.length==1 ? retrs[0] : retrs;
}
};

Expand Down Expand Up @@ -207,18 +209,18 @@ function watch(fpath, options, cb) {
&& options.maxSymLevel--)) {
return;
}
// Due to the unstable fs.watch(), if the `fpath` is a file then
// Due to the unstalbe fs.watch(), if the `fpath` is a file then
// switch to watch its parent directory instead of watch it directly.
// Once the logged filename matches it then triggers the callback function.
if (is.file(fpath)) {
var parent = path.resolve(fpath, '..');
fs.watch(parent, options, function(evt, fname) {
return fs.watch(parent, options, function(evt, fname) {
if (path.basename(fpath) === fname) {
normalizeCall(fpath, options, cb);
}
}).on('error', catchException);
} else if (is.dir(fpath)) {
fs.watch(fpath, options, function(evt, fname) {
var retr = fs.watch(fpath, options, function(evt, fname) {
if (fname) {
normalizeCall(path.join(fpath, fname), options, cb);
}
Expand All @@ -230,6 +232,7 @@ function watch(fpath, options, cb) {
watch(dir, options, cb);
});
}
return retr;
}
};

Expand All @@ -242,4 +245,3 @@ module.exports = handleOptions(watch, {
, followSymLinks: false
, maxSymLevel: 1
});

0 comments on commit 22e5936

Please sign in to comment.