Skip to content

Commit

Permalink
fix(file-list): Use modified throttle instead of debounce
Browse files Browse the repository at this point in the history
Closes #1545
  • Loading branch information
dignifiedquire committed Aug 5, 2015
1 parent 8197632 commit cb2aafb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
14 changes: 8 additions & 6 deletions lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ var List = function (patterns, excludes, emitter, preprocess, batchInterval) {
// Emit the `file_list_modified` event.
// This function is debounced to the value of `batchInterval`
// to avoid spamming the listener.
this._emitModified = _.debounce(function () {
this._emitModified = function () {
self._emitter.emit('file_list_modified', self.files)
}, this._batchInterval, {
leading: false,
maxWait: 2000,
trailing: true
})

self._emitModified = _.throttle(function () {
self._emitter.emit('file_list_modified', self.files)
}, self._batchInterval, {
leading: false
})
}
}

// Private Interface
Expand Down
37 changes: 21 additions & 16 deletions test/unit/file-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,14 @@ describe('FileList', () => {
})

it('fires "file_list_modified"', () => {
sinon.spy(list, '_emitModified')
var modified = sinon.stub()
emitter.on('file_list_modified', modified)

return list.refresh().then(() => {
list._emitModified.reset()
modified.reset()

return list.addFile('/some/d.js').then(() => {
expect(list._emitModified).to.have.been.calledOnce
expect(modified).to.have.been.calledOnce
})
})
})
Expand Down Expand Up @@ -516,15 +517,15 @@ describe('FileList', () => {
it('updates mtime and fires "file_list_modified"', () => {
// MATCH: /some/a.js, /some/b.js
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)

sinon.spy(list, '_emitModified')
var modified = sinon.stub()
emitter.on('file_list_modified', modified)

return list.refresh().then(files => {
mockFs._touchFile('/some/b.js', '2020-01-01')
list._emitModified.reset()
modified.reset()

return list.changeFile('/some/b.js').then(files => {
expect(list._emitModified).to.have.been.calledOnce
expect(modified).to.have.been.calledOnce
expect(findFile('/some/b.js', files.served).mtime).to.be.eql(new Date('2020-01-01'))
})
})
Expand All @@ -534,14 +535,15 @@ describe('FileList', () => {
// MATCH: /some/a.js
list = new List(patterns('/some/*.js', '/a.*'), ['/some/b.js'], emitter, preprocess)

sinon.spy(list, '_emitModified')
var modified = sinon.stub()
emitter.on('file_list_modified', modified)

return list.refresh().then(files => {
mockFs._touchFile('/some/b.js', '2020-01-01')
list._emitModified.reset()
modified.reset()

return list.changeFile('/some/b.js').then(() => {
expect(list._emitModified).to.not.have.been.called
expect(modified).to.not.have.been.called
})
})
})
Expand All @@ -551,13 +553,15 @@ describe('FileList', () => {
// MATCH: /some/a.js, /some/b.js, /a.txt
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)

sinon.spy(list, '_emitModified')
var modified = sinon.stub()
emitter.on('file_list_modified', modified)

return list.refresh().then(files => {
// not touching the file, stat will return still the same
list._emitModified.reset()
modified.reset()

return list.changeFile('/some/b.js').then(() => {
expect(list._emitModified).not.to.have.been.called
expect(modified).not.to.have.been.called
})
})
})
Expand Down Expand Up @@ -606,17 +610,18 @@ describe('FileList', () => {
// MATCH: /some/a.js, /some/b.js, /a.txt
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)

sinon.spy(list, '_emitModified')
var modified = sinon.stub()
emitter.on('file_list_modified', modified)

return list.refresh().then(files => {
list._emitModified.reset()
modified.reset()
return list.removeFile('/some/a.js')
}).then(files => {
expect(pathsFrom(files.served)).to.be.eql([
'/some/b.js',
'/a.txt'
])
expect(list._emitModified).to.have.been.calledOnce
expect(modified).to.have.been.calledOnce
})
})

Expand Down

0 comments on commit cb2aafb

Please sign in to comment.