Commit 68a5d608 authored by Leo Iannacone's avatar Leo Iannacone

Imported Upstream version 0.5.1

parents
support
test
examples
*.sock
0.5.1 / 2014-06-19
==================
* add repository field to readme (exciting)
0.5.0 / 2013-07-29
==================
* add `.throws(true)` to opt-in to responding with an array of error objects
* make `new` optional
0.4.0 / 2013-06-05
==================
* add catching of immediate callback errors
0.3.2 / 2013-03-15
==================
* remove Emitter call in constructor
0.3.1 / 2013-03-13
==================
* add Emitter() mixin for client. Closes #8
0.3.0 / 2013-03-13
==================
* add component.json
* add result example
* add .concurrency support
* add concurrency example
* add parallel example
0.2.1 / 2012-11-08
==================
* add .start, .end, and .duration properties
* change dependencies to devDependencies
0.2.0 / 2012-10-04
==================
* add progress events. Closes #5 (__BREAKING CHANGE__)
0.1.1 / 2012-07-03
==================
* change "complete" event to "progress"
0.1.0 / 2012-07-03
==================
* add Emitter inheritance and emit "complete" [burcu]
0.0.3 / 2012-06-02
==================
* Callback results should be in the order of the queued functions.
0.0.2 / 2012-02-12
==================
* any node
0.0.1 / 2010-01-03
==================
* Initial release
test:
@./node_modules/.bin/mocha \
--require should
.PHONY: test
\ No newline at end of file
# batch
Simple async batch with concurrency control and progress reporting.
## Installation
```
$ npm install batch
```
## API
```js
var Batch = require('batch')
, batch = new Batch;
batch.concurrency(4);
ids.forEach(function(id){
batch.push(function(done){
User.get(id, done);
});
});
batch.on('progress', function(e){
});
batch.end(function(err, users){
});
```
### Progress events
Contain the "job" index, response value, duration information, and completion data.
```js
{ index: 1,
value: 'bar',
pending: 2,
total: 3,
complete: 2,
percent: 66,
start: Thu Oct 04 2012 12:25:53 GMT-0700 (PDT),
end: Thu Oct 04 2012 12:25:53 GMT-0700 (PDT),
duration: 0 }
```
## License
(The MIT License)
Copyright (c) 2013 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "batch",
"repo": "visionmedia/batch",
"description": "Async task batching",
"version": "0.5.1",
"keywords": ["batch", "async", "utility", "concurrency", "concurrent"],
"dependencies": {
"component/emitter": "*"
},
"development": {},
"scripts": [
"index.js"
]
}
/**
* Module dependencies.
*/
var Batch = require('..');
var n = 10;
var batch = new Batch;
batch.concurrency(2);
while (n--) {
(function(n){
batch.push(function(done){
console.log(' start : %s', n);
setTimeout(function(){
console.log(' done : %s', n);
done();
}, 200);
})
})(n);
}
batch.end();
/**
* Module dependencies.
*/
var Batch = require('..');
var n = 10;
var batch = new Batch;
while (n--) {
(function(n){
batch.push(function(done){
console.log(' start : %s', n);
setTimeout(function(){
console.log(' done : %s', n);
done();
}, 200);
})
})(n);
}
batch.end();
/**
* Module dependencies.
*/
var Batch = require('..');
var n = 10;
var batch = new Batch;
while (n--) {
(function(n){
batch.push(function(done){
setTimeout(function(){
done(null, n);
}, 100);
})
})(n);
}
batch.end(function(err, res){
console.log(res);
});
/**
* Module dependencies.
*/
var Batch = require('..');
var n = 10;
var batch = new Batch;
batch.concurrency(1);
while (n--) {
(function(n){
batch.push(function(done){
console.log(' start : %s', n);
setTimeout(function(){
console.log(' done : %s', n);
done();
}, 200);
})
})(n);
}
batch.end();
/**
* Module dependencies.
*/
try {
var EventEmitter = require('events').EventEmitter;
} catch (err) {
var Emitter = require('emitter');
}
/**
* Noop.
*/
function noop(){}
/**
* Expose `Batch`.
*/
module.exports = Batch;
/**
* Create a new Batch.
*/
function Batch() {
if (!(this instanceof Batch)) return new Batch;
this.fns = [];
this.concurrency(Infinity);
this.throws(true);
for (var i = 0, len = arguments.length; i < len; ++i) {
this.push(arguments[i]);
}
}
/**
* Inherit from `EventEmitter.prototype`.
*/
if (EventEmitter) {
Batch.prototype.__proto__ = EventEmitter.prototype;
} else {
Emitter(Batch.prototype);
}
/**
* Set concurrency to `n`.
*
* @param {Number} n
* @return {Batch}
* @api public
*/
Batch.prototype.concurrency = function(n){
this.n = n;
return this;
};
/**
* Queue a function.
*
* @param {Function} fn
* @return {Batch}
* @api public
*/
Batch.prototype.push = function(fn){
this.fns.push(fn);
return this;
};
/**
* Set wether Batch will or will not throw up.
*
* @param {Boolean} throws
* @return {Batch}
* @api public
*/
Batch.prototype.throws = function(throws) {
this.e = !!throws;
return this;
};
/**
* Execute all queued functions in parallel,
* executing `cb(err, results)`.
*
* @param {Function} cb
* @return {Batch}
* @api public
*/
Batch.prototype.end = function(cb){
var self = this
, total = this.fns.length
, pending = total
, results = []
, errors = []
, cb = cb || noop
, fns = this.fns
, max = this.n
, throws = this.e
, index = 0
, done;
// empty
if (!fns.length) return cb(null, results);
// process
function next() {
var i = index++;
var fn = fns[i];
if (!fn) return;
var start = new Date;
try {
fn(callback);
} catch (err) {
callback(err);
}
function callback(err, res){
if (done) return;
if (err && throws) return done = true, cb(err);
var complete = total - pending + 1;
var end = new Date;
results[i] = res;
errors[i] = err;
self.emit('progress', {
index: i,
value: res,
error: err,
pending: pending,
total: total,
complete: complete,
percent: complete / total * 100 | 0,
start: start,
end: end,
duration: end - start
});
if (--pending) next();
else if(!throws) cb(errors, results);
else cb(null, results);
}
}
// concurrency
for (var i = 0; i < fns.length; i++) {
if (i == max) break;
next();
}
return this;
};
{
"name": "batch",
"version": "0.5.1",
"description": "Simple async batch",
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"devDependencies": {
"mocha": "*",
"should": "*"
},
"main": "index",
"repository": {
"type": "git",
"url": "https://github.com/visionmedia/batch.git"
}
}
var Batch = require('../');
var assert = require('assert');
describe('Batch', function(){
var batch;
beforeEach(function(){
batch = new Batch;
})
describe('#end(callback)', function(){
describe('when no functions are queued', function(){
it('should invoke the callback', function(done){
batch.end(done);
})
})
it('construct an array of results in order', function(done){
batch.push(function(fn){
setTimeout(function(){
fn(null, 'foo');
}, 100);
});
batch.push(function(fn){
setTimeout(function(){
fn(null, 'bar');
}, 50);
});
batch.end(function(err, res){
if (err) return done(err);
res.should.eql(['foo', 'bar']);
done();
})
})
describe('when several functions are queued', function(){
it('should invoke the callback', function(done){
batch.push(function(fn){
process.nextTick(fn);
})
batch.push(function(fn){
process.nextTick(fn);
})
batch.end(done);
})
})
describe('when a queued function is completed', function(){
it('should emit "progress" events', function(done){
batch.push(function(fn){
fn(null, 'foo');
});
batch.push(function(fn){
fn(null, 'bar');
});
batch.push(function(fn){
fn(null, 'baz');
});
var pending = 3;
batch.on('progress', function(e){
switch (e.index) {
case 0:
e.value.should.equal('foo');
e.percent.should.be.a.Number;
e.total.should.be.a.Number;
e.complete.should.be.a.Number;
e.pending.should.be.a.Number;
e.duration.should.be.a.Number;
break;
case 1:
e.value.should.equal('bar');
e.percent.should.be.a.Number;
break;
case 2:
e.value.should.equal('baz');
e.percent.should.be.a.Number;
break;
}
--pending || done();
})
batch.end(function(err, res){
if (err) return done(err);
})
})
})
describe('when several errors occur', function(){
it('should invoke the callback with the first error', function(done){
batch.push(function(fn){
fn(new Error('fail one'));
})
batch.push(function(fn){
fn(new Error('fail two'));
})
batch.end(function(err){
err.message.should.equal('fail one');
done();
});
})
})
describe('when .throws(false) is in effect', function(){
it('errors should pile up', function(done){
batch.push(function(fn){
fn(null, 'foo');
});
batch.push(function(fn){
fn(new Error('fail one'));
});
batch.push(function(fn){
fn(null, 'bar');
});
batch.push(function(fn){
fn(new Error('fail two'));
});
batch.push(function(fn){
fn(null, 'baz');
});
batch.throws(false);
batch.end(function(err, res){
err.should.be.an.instanceOf(Array);
assert(null == err[0]);
assert('fail one' == err[1].message);
assert(null == err[2]);
assert('fail two' == err[3].message);
res.should.eql(['foo', undefined, 'bar', undefined, 'baz']);
done();
});
})
})
})
})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment