From 65f1ecad58782cd832febafabc8e01019402bd33 Mon Sep 17 00:00:00 2001 From: TrevDev Date: Fri, 7 Aug 2015 14:00:06 -0700 Subject: [PATCH] fix(file-list): refresh resolves before 'file_list_modified' event change emitModified to emit 'file_list_modified' immediately during refresh() to remove throttle and race condition during subsequent runner executions Closes #1550 --- lib/file-list.js | 18 ++++++++---------- test/unit/file-list.spec.js | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/file-list.js b/lib/file-list.js index 55efeb075..c0d9a9352 100644 --- a/lib/file-list.js +++ b/lib/file-list.js @@ -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 // ----------------- @@ -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 () { 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', () => {