batcher allow duplicate id if the job is pushed during flush

This commit is contained in:
Evan You 2014-10-27 12:03:35 -04:00
parent c57047d460
commit 8ac21629c0
2 changed files with 14 additions and 19 deletions

View File

@ -13,29 +13,23 @@ var p = Batcher.prototype
/**
* Push a job into the job queue.
* Jobs with duplicate IDs will be skipped, however we can
* use the `override` option to override existing jobs.
* Jobs with duplicate IDs will be skipped unless it's
* pushed when the queue is being flushed.
*
* @param {Object} job
* properties:
* - {String|Number} id
* - {Boolean} override
* - {Function} run
*/
p.push = function (job) {
if (!job.id || !this.has[job.id]) {
if (!job.id || !this.has[job.id] || this.flushing) {
this.queue.push(job)
this.has[job.id] = job
if (!this.waiting) {
this.waiting = true
_.nextTick(this.flush, this)
}
} else if (job.override) {
var oldJob = this.has[job.id]
oldJob.cancelled = true
this.queue.push(job)
this.has[job.id] = job
}
}
@ -45,6 +39,7 @@ p.push = function (job) {
*/
p.flush = function () {
this.flushing = true
// do not cache length because more jobs might be pushed
// as we run existing jobs
for (var i = 0; i < this.queue.length; i++) {
@ -64,6 +59,7 @@ p.reset = function () {
this.has = {}
this.queue = []
this.waiting = false
this.flushing = false
}
module.exports = Batcher

View File

@ -35,20 +35,19 @@ describe('Batcher', function () {
})
})
it('override', function (done) {
var spy2 = jasmine.createSpy('batcher')
it('allow diplicate when flushing', function (done) {
batcher.push({
id: 1,
run: spy
})
batcher.push({
id: 1,
run: spy2,
override: true
run: function () {
spy()
batcher.push({
id: 1,
run: spy
})
}
})
nextTick(function () {
expect(spy).not.toHaveBeenCalled()
expect(spy2.calls.count()).toBe(1)
expect(spy.calls.count()).toBe(2)
done()
})
})