Permalink
Browse files

Initial commit

  • Loading branch information...
tj committed Jan 17, 2012
0 parents commit 8ce1e5e491d7f575d575b03299bc0f54d62b1e94
Showing with 185 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +4 −0 .npmignore
  3. +5 −0 History.md
  4. +6 −0 Makefile
  5. +29 −0 Readme.md
  6. +2 −0 index.js
  7. +58 −0 lib/batch.js
  8. +13 −0 package.json
  9. +65 −0 test/batch.js
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+*.sock
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
@@ -0,0 +1,6 @@
+
+test:
+ @./node_modules/.bin/mocha \
+ --require should
+
+.PHONY: test
@@ -0,0 +1,29 @@
+
+# batch
+
+ Simple async batch
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 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.
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/batch');
@@ -0,0 +1,58 @@
+
+/**
+ * Expose `Batch`.
+ */
+
+module.exports = Batch;
+
+/**
+ * Create a new Batch.
+ */
+
+function Batch() {
+ this.fns = [];
+ for (var i = 0, len = arguments.length; i < len; ++i) {
+ this.push(arguments[i]);
+ }
+}
+
+/**
+ * Queue a function.
+ *
+ * @param {Function} fn
+ * @return {Batch}
+ * @api public
+ */
+
+Batch.prototype.push = function(fn){
+ this.fns.push(fn);
+ 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 pending = this.fns.length
+ , results = []
+ , done;
+
+ if (!this.fns.length) return cb(null, results);
+
+ this.fns.forEach(function(fn){
+ fn(function(err, res){
+ if (done) return;
+ if (err) return done = true, cb(err);
+ results.push(res);
+ --pending || cb(null, results);
+ });
+ });
+
+ return this;
+};
@@ -0,0 +1,13 @@
+{
+ "name": "batch"
+ , "version": "0.0.1"
+ , "description": "Simple async batch"
+ , "keywords": []
+ , "author": "TJ Holowaychuk <tj@vision-media.ca>"
+ , "dependencies": {
+ "mocha": "*"
+ , "should": "*"
+ }
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}
@@ -0,0 +1,65 @@
+
+var Batch = require('../');
+
+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', function(done){
+ batch.push(function(fn){
+ fn(null, 'foo');
+ });
+
+ batch.push(function(fn){
+ fn(null, 'bar');
+ });
+
+ 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 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();
+ });
+ })
+ })
+ })
+})

0 comments on commit 8ce1e5e

Please sign in to comment.