Skip to content

Commit

Permalink
fix(file-list): refresh resolves before 'file_list_modified' event
Browse files Browse the repository at this point in the history
change emitModified to emit 'file_list_modified' immediately
during refresh() to remove throttle and race condition during
subsequent runner executions

Closes #1550
  • Loading branch information
TrevDev committed Aug 9, 2015
1 parent 6c6c867 commit 65f1eca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
18 changes: 8 additions & 10 deletions lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,17 @@ var List = function (patterns, excludes, emitter, preprocess, batchInterval) {
var self = this

// Emit the `file_list_modified` event.
// This function is debounced to the value of `batchInterval`
// This function is throttled to the value of `batchInterval`
// to avoid spamming the listener.
this._emitModified = function () {
function emit () {
self._emitter.emit('file_list_modified', self.files)

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

}
// Private Interface
// -----------------

Expand Down Expand Up @@ -203,7 +201,7 @@ List.prototype._refresh = function () {
.cancellable()
.then(function () {
self.buckets = buckets
self._emitModified()
self._emitModified(true)
return self.files
})
.catch(Promise.CancellationError, function () {
Expand Down
16 changes: 15 additions & 1 deletion test/unit/file-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('FileList', () => {
fs: mockFs
})

list = new List(patterns('/some/*.js', '*.txt'), [], emitter, preprocess)
list = new List(patterns('/some/*.js', '*.txt'), [], emitter, preprocess, 100)
})

it('resolves patterns', () => {
Expand Down Expand Up @@ -355,6 +355,20 @@ describe('FileList', () => {
expect(err.message).to.be.eql('failing')
})
})

it('fires modified before resolving promise after subsequent calls', () => {
var modified = sinon.stub()
emitter.on('file_list_modified', modified)

return list.refresh().then(() => {
expect(modified).to.have.been.calledOnce
})
.then(() => {
list.refresh().then(() => {
expect(modified).to.have.been.calledTwice
})
})
})
})

describe('reload', () => {
Expand Down

0 comments on commit 65f1eca

Please sign in to comment.