Skip to content

Commit cb2aafb

Browse files
fix(file-list): Use modified throttle instead of debounce
Closes #1545
1 parent 8197632 commit cb2aafb

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

lib/file-list.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ var List = function (patterns, excludes, emitter, preprocess, batchInterval) {
7373
// Emit the `file_list_modified` event.
7474
// This function is debounced to the value of `batchInterval`
7575
// to avoid spamming the listener.
76-
this._emitModified = _.debounce(function () {
76+
this._emitModified = function () {
7777
self._emitter.emit('file_list_modified', self.files)
78-
}, this._batchInterval, {
79-
leading: false,
80-
maxWait: 2000,
81-
trailing: true
82-
})
78+
79+
self._emitModified = _.throttle(function () {
80+
self._emitter.emit('file_list_modified', self.files)
81+
}, self._batchInterval, {
82+
leading: false
83+
})
84+
}
8385
}
8486

8587
// Private Interface

test/unit/file-list.spec.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,14 @@ describe('FileList', () => {
436436
})
437437

438438
it('fires "file_list_modified"', () => {
439-
sinon.spy(list, '_emitModified')
439+
var modified = sinon.stub()
440+
emitter.on('file_list_modified', modified)
440441

441442
return list.refresh().then(() => {
442-
list._emitModified.reset()
443+
modified.reset()
443444

444445
return list.addFile('/some/d.js').then(() => {
445-
expect(list._emitModified).to.have.been.calledOnce
446+
expect(modified).to.have.been.calledOnce
446447
})
447448
})
448449
})
@@ -516,15 +517,15 @@ describe('FileList', () => {
516517
it('updates mtime and fires "file_list_modified"', () => {
517518
// MATCH: /some/a.js, /some/b.js
518519
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
519-
520-
sinon.spy(list, '_emitModified')
520+
var modified = sinon.stub()
521+
emitter.on('file_list_modified', modified)
521522

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

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

537-
sinon.spy(list, '_emitModified')
538+
var modified = sinon.stub()
539+
emitter.on('file_list_modified', modified)
538540

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

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

554-
sinon.spy(list, '_emitModified')
556+
var modified = sinon.stub()
557+
emitter.on('file_list_modified', modified)
555558

556559
return list.refresh().then(files => {
557560
// not touching the file, stat will return still the same
558-
list._emitModified.reset()
561+
modified.reset()
562+
559563
return list.changeFile('/some/b.js').then(() => {
560-
expect(list._emitModified).not.to.have.been.called
564+
expect(modified).not.to.have.been.called
561565
})
562566
})
563567
})
@@ -606,17 +610,18 @@ describe('FileList', () => {
606610
// MATCH: /some/a.js, /some/b.js, /a.txt
607611
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
608612

609-
sinon.spy(list, '_emitModified')
613+
var modified = sinon.stub()
614+
emitter.on('file_list_modified', modified)
610615

611616
return list.refresh().then(files => {
612-
list._emitModified.reset()
617+
modified.reset()
613618
return list.removeFile('/some/a.js')
614619
}).then(files => {
615620
expect(pathsFrom(files.served)).to.be.eql([
616621
'/some/b.js',
617622
'/a.txt'
618623
])
619-
expect(list._emitModified).to.have.been.calledOnce
624+
expect(modified).to.have.been.calledOnce
620625
})
621626
})
622627

0 commit comments

Comments
 (0)