| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- var _ = require('./util')
- /**
- * The Batcher maintains a job queue to be run
- * async on the next event loop.
- */
- function Batcher () {
- this._preFlush = null
- this.reset()
- }
- 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.
- *
- * @param {Object} job
- * properties:
- * - {String|Number} id
- * - {Boolean} override
- * - {Function} run
- */
- p.push = function (job) {
- if (!job.id || !this.has[job.id]) {
- 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
- }
- }
- /**
- * Flush the queue and run the jobs.
- * Will call a preFlush hook if has one.
- */
- p.flush = function () {
- // before flush hook
- if (this._preFlush) {
- this._preFlush()
- }
- // 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
- }
- module.exports = Batcher
|