diff --git a/lib/file-list.js b/lib/file-list.js index 55efeb075..73cf8d699 100644 --- a/lib/file-list.js +++ b/lib/file-list.js @@ -70,20 +70,20 @@ 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` - // to avoid spamming the listener. - this._emitModified = function () { - self._emitter.emit('file_list_modified', self.files) - - self._emitModified = _.throttle(function () { + // Emit the `file_list_modified` event. + // This function is throttled to the value of `batchInterval` + // to avoid spamming the listener. + ;(function () { + function emit () { 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 // ----------------- @@ -203,7 +203,7 @@ List.prototype._refresh = function () { .cancellable() .then(function () { self.buckets = buckets - self._emitModified() + self._emitModified(true) return self.files }) .catch(Promise.CancellationError, function () { diff --git a/test/unit/file-list.spec.js b/test/unit/file-list.spec.js index db6febcde..7e3b083bc 100644 --- a/test/unit/file-list.spec.js +++ b/test/unit/file-list.spec.js @@ -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', () => { @@ -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', () => {