6 var EventEmitter = require('events').EventEmitter;
8 var Emitter = require('emitter');
21 module.exports = Batch;
28 if (!(this instanceof Batch)) return new Batch;
30 this.concurrency(Infinity);
32 for (var i = 0, len = arguments.length; i < len; ++i) {
33 this.push(arguments[i]);
38 * Inherit from `EventEmitter.prototype`.
42 Batch.prototype.__proto__ = EventEmitter.prototype;
44 Emitter(Batch.prototype);
48 * Set concurrency to `n`.
55 Batch.prototype.concurrency = function(n){
63 * @param {Function} fn
68 Batch.prototype.push = function(fn){
74 * Set wether Batch will or will not throw up.
76 * @param {Boolean} throws
80 Batch.prototype.throws = function(throws) {
86 * Execute all queued functions in parallel,
87 * executing `cb(err, results)`.
89 * @param {Function} cb
94 Batch.prototype.end = function(cb){
96 , total = this.fns.length
108 if (!fns.length) return cb(null, results);
115 var start = new Date;
123 function callback(err, res){
125 if (err && throws) return done = true, cb(err);
126 var complete = total - pending + 1;
132 self.emit('progress', {
139 percent: complete / total * 100 | 0,
142 duration: end - start
145 if (--pending) next();
146 else if(!throws) cb(errors, results);
147 else cb(null, results);
152 for (var i = 0; i < fns.length; i++) {