Skip to content

Commit

Permalink
fix(watcher): ignore double "add" events
Browse files Browse the repository at this point in the history
On linux, `fs.watch` (which chokidar uses when `usePolling: false`)
fires "add" events twice. This change makes `fileList` to ignore the
second event.
  • Loading branch information
vojtajina committed Oct 22, 2013
1 parent dde1da4 commit 6cbaac7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,16 @@ var List = function(patterns, excludes, emitter, preprocess, batchInterval) {
return done();
}

var addedFile = new File(path);
buckets[i].push(addedFile);

return fs.stat(path, function(err, stat) {
// in the case someone refresh() the list before stat callback
if (self.buckets === buckets) {
addedFile.mtime = stat.mtime;

var file = new File(path, stat.mtime);
return preprocess(file, function() {
return preprocess(addedFile, function() {
// TODO(vojta): ignore if refresh/reload happens
buckets[i].push(file);
log.info('Added file "%s".', path);
fireEventAndDefer();
done();
Expand Down
24 changes: 24 additions & 0 deletions test/unit/file-list.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,30 @@ describe 'file-list', ->
done()


it 'should ignore very quick double "add"', (done) ->
# On linux fs.watch (chokidar with usePolling: false) fires "add" event twice.
# This checks that we only stat and preprocess the file once.

sinon.spy mockFs, 'stat'
list = new m.List patterns('/a.*'), [], emitter, preprocessMock

pending = 2
finish = ->
pending--
if pending is 0
expect(preprocessMock).to.have.been.calledOnce
expect(mockFs.stat).to.have.been.calledOnce
done()

refreshListAndThen (files) ->
preprocessMock.reset()
mockFs.stat.reset()

list.addFile '/a.js', finish
# fire again, before the stat gets back
list.addFile '/a.js', finish


it 'should set proper mtime of new file', (done) ->
list = new m.List patterns('/a.*'), [], emitter, preprocessMock

Expand Down

0 comments on commit 6cbaac7

Please sign in to comment.