Skip to content

Commit

Permalink
Merge pull request #1551 from thetrevdev/fix-file-list-refresh-resolv…
Browse files Browse the repository at this point in the history
…es-before-modified-event

fix(file-list): refresh resolves before 'file_list_modified' event
  • Loading branch information
dignifiedquire committed Aug 10, 2015
2 parents 6c6c867 + 65f1eca commit 0a9f6f7
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 0a9f6f7

Please sign in to comment.