mirror of https://github.com/vuejs/vue.git
				
				
				
			batcher allow duplicate id if the job is pushed during flush
This commit is contained in:
		
							parent
							
								
									c57047d460
								
							
						
					
					
						commit
						8ac21629c0
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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: function () {
 | 
			
		||||
        spy()
 | 
			
		||||
        batcher.push({
 | 
			
		||||
          id: 1,
 | 
			
		||||
          run: spy
 | 
			
		||||
        })
 | 
			
		||||
    batcher.push({
 | 
			
		||||
      id: 1,
 | 
			
		||||
      run: spy2,
 | 
			
		||||
      override: true
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
    nextTick(function () {
 | 
			
		||||
      expect(spy).not.toHaveBeenCalled()
 | 
			
		||||
      expect(spy2.calls.count()).toBe(1)
 | 
			
		||||
      expect(spy.calls.count()).toBe(2)
 | 
			
		||||
      done()
 | 
			
		||||
    })
 | 
			
		||||
  })
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue