Skip to content

Commit

Permalink
improve batcher comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 5, 2014
1 parent 714c1a1 commit d2e33d4
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/batcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@ var _ = require('./util')

/**
* The Batcher maintains a job queue to be run
* async on the next event loop.
* async on the next event loop. A "job" can be any object
* that implements the following interface:
*
* {
* id: {Number} - optional
* run: {Function}
* user: {Boolean} - optional
* }
*
* The `id` property is used to prevent duplication of jobs,
* while jobs with `user:true` need to be processed after
* all internal jobs have been processed first.
*
* In most cases a job will actually be a Watcher instance
* which implements the above interface.
*/

function Batcher () {
Expand Down Expand Up @@ -68,8 +82,8 @@ p.push = function (job) {

p.flush = function () {
this.flushing = true
this.run(this.queue)
this.run(this.userQueue)
run(this.queue)
run(this.userQueue)
this.reset()
}

Expand All @@ -79,14 +93,11 @@ p.flush = function () {
* @param {Array} queue
*/

p.run = function (queue) {
function run (queue) {
// do not cache length because more jobs might be pushed
// as we run existing jobs
for (var i = 0; i < queue.length; i++) {
var job = queue[i]
if (!job.cancelled) {
job.run()
}
queue[i].run()
}
}

Expand Down

0 comments on commit d2e33d4

Please sign in to comment.