Sha256: bd1ac09773e16539a6c485855e0624605d3e09ce3d094b47d6f090dbe6007fb2

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

var _ = require('./util')

/**
 * The Batcher maintains a job queue to be run
 * async on the next event loop.
 */

function Batcher () {
  this.reset()
}

var p = Batcher.prototype

/**
 * Push a job into the job queue.
 * Jobs with duplicate IDs will be skipped unless it's
 * pushed when the queue is being flushed.
 *
 * @param {Object} job
 *   properties:
 *   - {String|Number} id
 *   - {Function}      run
 */

p.push = function (job) {
  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)
    }
  }
}

/**
 * Flush the queue and run the jobs.
 * Will call a preFlush hook if has one.
 */

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++) {
    var job = this.queue[i]
    if (!job.cancelled) {
      job.run()
    }
  }
  this.reset()
}

/**
 * Reset the batcher's state.
 */

p.reset = function () {
  this.has = {}
  this.queue = []
  this.waiting = false
  this.flushing = false
}

module.exports = Batcher

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fluentd-ui-0.3.13 vendor/assets/javascripts/bower/vue/src/batcher.js
fluentd-ui-0.3.12 vendor/assets/javascripts/bower/vue/src/batcher.js
fluentd-ui-0.3.11 vendor/assets/javascripts/bower/vue/src/batcher.js
fluentd-ui-0.3.10 vendor/assets/javascripts/bower/vue/src/batcher.js
fluentd-ui-0.3.9 vendor/assets/javascripts/bower/vue/src/batcher.js