Skip to content

Commit 655599a

Browse files
fix(file-list): Ensure autowatchDelay is working.
Closes #1520.
1 parent def226f commit 655599a

2 files changed

Lines changed: 59 additions & 52 deletions

File tree

lib/file-list.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ var List = function (patterns, excludes, emitter, preprocess, batchInterval) {
7171
var self = this
7272

7373
// Emit the `file_list_modified` event.
74-
// This function is throttled to the value of `batchInterval`
74+
// This function is debounced to the value of `batchInterval`
7575
// to avoid spamming the listener.
76-
this._emitModified = _.throttle(function () {
76+
this._emitModified = _.debounce(function () {
7777
self._emitter.emit('file_list_modified', self.files)
78-
}, this._batchInterval)
78+
}, this._batchInterval, {
79+
leading: false,
80+
maxWait: 2000,
81+
trailing: true
82+
})
7983
}
8084

8185
// Private Interface

test/unit/file-list.spec.js

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

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

442441
return list.refresh().then(() => {
443-
expect(modified).to.have.been.calledOnce
444-
modified.reset()
442+
list._emitModified.reset()
445443

446444
return list.addFile('/some/d.js').then(() => {
447-
expect(modified).to.have.been.calledOnce
445+
expect(list._emitModified).to.have.been.calledOnce
448446
})
449447
})
450448
})
@@ -453,24 +451,17 @@ describe('FileList', () => {
453451
// On linux fs.watch (chokidar with usePolling: false) fires "add" event twice.
454452
// This checks that we only stat and preprocess the file once.
455453

456-
modified = sinon.stub()
457-
emitter.on('file_list_modified', modified)
458-
459454
return list.refresh().then(() => {
460-
expect(modified).to.have.been.calledOnce
461-
modified.reset()
462455
preprocess.reset()
463456
sinon.spy(mockFs, 'stat')
464457

465458
return Promise.all([
466459
list.addFile('/some/d.js'),
467460
list.addFile('/some/d.js')
468461
]).then(() => {
469-
expect(modified).to.have.been.calledOnce
470462
expect(preprocess).to.have.been.calledOnce
471463
expect(mockFs.stat).to.have.been.calledOnce
472-
}
473-
)
464+
})
474465
})
475466
})
476467

@@ -520,20 +511,20 @@ describe('FileList', () => {
520511

521512
mockFs._touchFile('/some/a.js', '2012-04-04')
522513
mockFs._touchFile('/some/b.js', '2012-05-05')
523-
524-
modified = sinon.stub()
525-
emitter.on('file_list_modified', modified)
526514
})
527515

528516
it('updates mtime and fires "file_list_modified"', () => {
529517
// MATCH: /some/a.js, /some/b.js
530518
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
519+
520+
sinon.spy(list, '_emitModified')
521+
531522
return list.refresh().then(files => {
532523
mockFs._touchFile('/some/b.js', '2020-01-01')
533-
modified.reset()
524+
list._emitModified.reset()
534525

535526
return list.changeFile('/some/b.js').then(files => {
536-
expect(modified).to.have.been.called
527+
expect(list._emitModified).to.have.been.calledOnce
537528
expect(findFile('/some/b.js', files.served).mtime).to.be.eql(new Date('2020-01-01'))
538529
})
539530
})
@@ -543,12 +534,14 @@ describe('FileList', () => {
543534
// MATCH: /some/a.js
544535
list = new List(patterns('/some/*.js', '/a.*'), ['/some/b.js'], emitter, preprocess)
545536

537+
sinon.spy(list, '_emitModified')
538+
546539
return list.refresh().then(files => {
547540
mockFs._touchFile('/some/b.js', '2020-01-01')
548-
modified.reset()
541+
list._emitModified.reset()
549542

550543
return list.changeFile('/some/b.js').then(() => {
551-
expect(modified).to.not.have.been.called
544+
expect(list._emitModified).to.not.have.been.called
552545
})
553546
})
554547
})
@@ -558,11 +551,13 @@ describe('FileList', () => {
558551
// MATCH: /some/a.js, /some/b.js, /a.txt
559552
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
560553

554+
sinon.spy(list, '_emitModified')
555+
561556
return list.refresh().then(files => {
562557
// not touching the file, stat will return still the same
563-
modified.reset()
558+
list._emitModified.reset()
564559
return list.changeFile('/some/b.js').then(() => {
565-
expect(modified).not.to.have.been.called
560+
expect(list._emitModified).not.to.have.been.called
566561
})
567562
})
568563
})
@@ -611,15 +606,17 @@ describe('FileList', () => {
611606
// MATCH: /some/a.js, /some/b.js, /a.txt
612607
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess)
613608

609+
sinon.spy(list, '_emitModified')
610+
614611
return list.refresh().then(files => {
615-
modified.reset()
616-
return list.removeFile('/some/a.js').then(files => {
617-
expect(pathsFrom(files.served)).to.be.eql([
618-
'/some/b.js',
619-
'/a.txt'
620-
])
621-
expect(modified).to.have.been.calledOnce
622-
})
612+
list._emitModified.reset()
613+
return list.removeFile('/some/a.js')
614+
}).then(files => {
615+
expect(pathsFrom(files.served)).to.be.eql([
616+
'/some/b.js',
617+
'/a.txt'
618+
])
619+
expect(list._emitModified).to.have.been.calledOnce
623620
})
624621
})
625622

@@ -642,6 +639,7 @@ describe('FileList', () => {
642639
beforeEach(() => {
643640
patternList = PATTERN_LIST
644641
mg = MG
642+
Promise.setScheduler(fn => fn())
645643

646644
preprocess = sinon.spy((file, done) => process.nextTick(done))
647645
emitter = new EventEmitter()
@@ -663,17 +661,19 @@ describe('FileList', () => {
663661
List = proxyquire('../../lib/file-list', {
664662
helper: helper,
665663
glob: glob,
666-
fs: mockFs
664+
fs: mockFs,
665+
bluebird: Promise
667666
})
668667
})
669668

670669
afterEach(() => {
671670
clock.restore()
671+
Promise.setScheduler(fn => process.nextTick(fn))
672672
})
673673

674-
it('batches multiple changes within an interval', done => {
674+
it('batches multiple changes within an interval', () => {
675675
// MATCH: /some/a.js, /some/b.js, /a.txt
676-
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess, 1000)
676+
list = new List(patterns('/some/*.js', '/a.*'), [], emitter, preprocess, 100)
677677

678678
return list.refresh().then(files => {
679679
modified.reset()
@@ -684,39 +684,42 @@ describe('FileList', () => {
684684
list.addFile('/a.txt') // /some/b.js, /a.txt
685685
list.addFile('/some/0.js') // /some/0.js, /some/b.js, /a.txt
686686

687-
clock.tick(999)
687+
clock.tick(99)
688688
expect(modified).to.not.have.been.called
689-
emitter.once('file_list_modified', files => {
690-
expect(pathsFrom(files.served)).to.be.eql([
691-
'/some/0.js',
692-
'/some/b.js',
693-
'/a.txt'
694-
])
695-
done()
696-
})
697689

698-
clock.tick(1001)
690+
clock.tick(2)
691+
expect(modified).to.have.been.calledOnce
692+
693+
files = modified.lastCall.args[0]
694+
expect(pathsFrom(files.served)).to.be.eql([
695+
'/some/0.js',
696+
'/some/b.js',
697+
'/a.txt'
698+
])
699699
})
700700
})
701701

702702
it('waits while file preprocessing, if the file was deleted and immediately added', done => {
703-
list = new List(patterns('/a.*'), [], emitter, preprocess, 1000)
703+
list = new List(patterns('/a.*'), [], emitter, preprocess, 100)
704704

705705
return list.refresh().then(files => {
706706
preprocess.reset()
707+
modified.reset()
707708

708709
// Remove and then immediately add file to the bucket
709710
list.removeFile('/a.txt')
710711
list.addFile('/a.txt')
711712

712-
clock.tick(1000)
713+
clock.tick(99)
714+
715+
expect(preprocess).to.not.have.been.called
713716

714-
emitter.once('file_list_modified', files => {
717+
emitter.once('file_list_modified', () => _.defer(() => {
715718
expect(preprocess).to.have.been.calledOnce
716-
return done()
717-
})
719+
done()
720+
}))
718721

719-
clock.tick(1001)
722+
clock.tick(2)
720723
})
721724
})
722725
})

0 commit comments

Comments
 (0)