Skip to content

Commit

Permalink
Add nomatch event. Closes GH-40.
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed Nov 28, 2013
1 parent 6648d01 commit bfd0ccc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ var gaze = new Gaze(pattern, options, callback);
* `renamed(newPath, oldPath)` When a file has been renamed.
* `end()` When the watcher is closed and watches have been removed.
* `error(err)` When an error occurs.
* `nomatch` When no files have been matched.

#### Methods

Expand Down
13 changes: 13 additions & 0 deletions lib/gaze.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ function Gaze(patterns, opts, done) {
this.add(patterns, done);
}

// keep the process alive
this._keepalive = setInterval(function() {}, 200);

return this;
}
util.inherits(Gaze, EE);
Expand Down Expand Up @@ -152,6 +155,7 @@ Gaze.prototype.close = function(_reset) {
setTimeout(function() {
self.emit('end');
self.removeAllListeners();
clearInterval(self._keepalive);
}, delay + 100);
}
return self;
Expand Down Expand Up @@ -331,6 +335,15 @@ Gaze.prototype._initWatched = function(done) {
var self = this;
var cwd = this.options.cwd || process.cwd();
var curWatched = Object.keys(self._watched);

// if no matching files
if (curWatched.length < 1) {
self.emit('ready', self);
if (done) { done.call(self, null, self); }
self.emit('nomatch');
return;
}

helper.forEachSeries(curWatched, function(dir, next) {
dir = dir || '';
var files = self._watched[dir];
Expand Down
12 changes: 11 additions & 1 deletion test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@ exports.api = {
this.close();
test.done();
});
}
},
nomatch: function(test) {
test.expect(1);
gaze('nomatch.js', function(err, watcher) {
watcher.on('nomatch', function() {
test.ok(true, 'nomatch was emitted.');
watcher.close();
});
watcher.on('end', test.done);
});
},
};

0 comments on commit bfd0ccc

Please sign in to comment.