Skip to content

Commit 8ac2162

Browse files
committed
batcher allow duplicate id if the job is pushed during flush
1 parent c57047d commit 8ac2162

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/batcher.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,23 @@ var p = Batcher.prototype
1313

1414
/**
1515
* Push a job into the job queue.
16-
* Jobs with duplicate IDs will be skipped, however we can
17-
* use the `override` option to override existing jobs.
16+
* Jobs with duplicate IDs will be skipped unless it's
17+
* pushed when the queue is being flushed.
1818
*
1919
* @param {Object} job
2020
* properties:
2121
* - {String|Number} id
22-
* - {Boolean} override
2322
* - {Function} run
2423
*/
2524

2625
p.push = function (job) {
27-
if (!job.id || !this.has[job.id]) {
26+
if (!job.id || !this.has[job.id] || this.flushing) {
2827
this.queue.push(job)
2928
this.has[job.id] = job
3029
if (!this.waiting) {
3130
this.waiting = true
3231
_.nextTick(this.flush, this)
3332
}
34-
} else if (job.override) {
35-
var oldJob = this.has[job.id]
36-
oldJob.cancelled = true
37-
this.queue.push(job)
38-
this.has[job.id] = job
3933
}
4034
}
4135

@@ -45,6 +39,7 @@ p.push = function (job) {
4539
*/
4640

4741
p.flush = function () {
42+
this.flushing = true
4843
// do not cache length because more jobs might be pushed
4944
// as we run existing jobs
5045
for (var i = 0; i < this.queue.length; i++) {
@@ -64,6 +59,7 @@ p.reset = function () {
6459
this.has = {}
6560
this.queue = []
6661
this.waiting = false
62+
this.flushing = false
6763
}
6864

6965
module.exports = Batcher

test/unit/specs/batcher_spec.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,19 @@ describe('Batcher', function () {
3535
})
3636
})
3737

38-
it('override', function (done) {
39-
var spy2 = jasmine.createSpy('batcher')
38+
it('allow diplicate when flushing', function (done) {
4039
batcher.push({
4140
id: 1,
42-
run: spy
43-
})
44-
batcher.push({
45-
id: 1,
46-
run: spy2,
47-
override: true
41+
run: function () {
42+
spy()
43+
batcher.push({
44+
id: 1,
45+
run: spy
46+
})
47+
}
4848
})
4949
nextTick(function () {
50-
expect(spy).not.toHaveBeenCalled()
51-
expect(spy2.calls.count()).toBe(1)
50+
expect(spy.calls.count()).toBe(2)
5251
done()
5352
})
5453
})

0 commit comments

Comments
 (0)