Skip to content

Commit

Permalink
Defers emitting nomatch/ready events. Fixes shamaGH-81. Closes shamaG…
Browse files Browse the repository at this point in the history
…H-82.

When there is no matching files to give a chance for clients of the
alternate interface (`new Gaze`) to attach event handlers.
  • Loading branch information
amasad authored and shama committed Feb 23, 2014
1 parent 650c70b commit d8f3699
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/gaze.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var fs = require('fs');
var path = require('path');
var globule = require('globule');
var helper = require('./helper');
var setImmediate = require('timers').setImmediate;

// globals
var delay = 10;
Expand Down Expand Up @@ -336,9 +337,12 @@ Gaze.prototype._initWatched = function(done) {

// if no matching files
if (curWatched.length < 1) {
self.emit('ready', self);
if (done) { done.call(self, null, self); }
self.emit('nomatch');
// Defer to emitting to give a chance to attach event handlers.
setImmediate(function () {
self.emit('ready', self);
if (done) { done.call(self, null, self); }
self.emit('nomatch');
});
return;
}

Expand Down
9 changes: 9 additions & 0 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ exports.api = {
this.close();
});
},
newGazeNomatch: function(test) {
test.expect(1);
var g = new gaze.Gaze('nomatch.js');
g.on('nomatch', function(watcher) {
test.ok(true, 'nomatch was emitted.');
this.on('end', test.done);
this.close();
});
},
nomatch: function(test) {
test.expect(1);
gaze('nomatch.js', function(err, watcher) {
Expand Down

0 comments on commit d8f3699

Please sign in to comment.