[Pkg-javascript-commits] [node-batch] 01/02: Imported Upstream version 0.5.1

Leo Iannacone l3on-guest at moszumanska.debian.org
Sun Jul 6 12:07:13 UTC 2014


This is an automated email from the git hooks/post-receive script.

l3on-guest pushed a commit to branch master
in repository node-batch.

commit 68a5d60883660eddd14ae42e7095fd311934cf14
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Sun Jul 6 13:51:58 2014 +0200

    Imported Upstream version 0.5.1
---
 .npmignore              |   4 ++
 History.md              |  71 ++++++++++++++++++++++
 Makefile                |   6 ++
 Readme.md               |  74 +++++++++++++++++++++++
 component.json          |  14 +++++
 examples/concurrency.js |  25 ++++++++
 examples/parallel.js    |  23 +++++++
 examples/results.js     |  23 +++++++
 examples/serial.js      |  25 ++++++++
 index.js                | 158 ++++++++++++++++++++++++++++++++++++++++++++++++
 package.json            |  15 +++++
 test/batch.js           | 151 +++++++++++++++++++++++++++++++++++++++++++++
 12 files changed, 589 insertions(+)

diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..f1250e5
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..424324a
--- /dev/null
+++ b/History.md
@@ -0,0 +1,71 @@
+
+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
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..634e372
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,6 @@
+
+test:
+	@./node_modules/.bin/mocha \
+		--require should
+
+.PHONY: test
\ No newline at end of file
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..f2345c6
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,74 @@
+
+# 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 at 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.
diff --git a/component.json b/component.json
new file mode 100644
index 0000000..4c6376c
--- /dev/null
+++ b/component.json
@@ -0,0 +1,14 @@
+{
+  "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"
+  ]
+}
diff --git a/examples/concurrency.js b/examples/concurrency.js
new file mode 100644
index 0000000..2a7d892
--- /dev/null
+++ b/examples/concurrency.js
@@ -0,0 +1,25 @@
+
+/**
+ * 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();
diff --git a/examples/parallel.js b/examples/parallel.js
new file mode 100644
index 0000000..0aa790d
--- /dev/null
+++ b/examples/parallel.js
@@ -0,0 +1,23 @@
+
+/**
+ * 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();
diff --git a/examples/results.js b/examples/results.js
new file mode 100644
index 0000000..a4c89ac
--- /dev/null
+++ b/examples/results.js
@@ -0,0 +1,23 @@
+
+/**
+ * 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);
+});
diff --git a/examples/serial.js b/examples/serial.js
new file mode 100644
index 0000000..4a8f84e
--- /dev/null
+++ b/examples/serial.js
@@ -0,0 +1,25 @@
+
+/**
+ * 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();
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..c2cbe46
--- /dev/null
+++ b/index.js
@@ -0,0 +1,158 @@
+/**
+ * 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;
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..137b3a6
--- /dev/null
+++ b/package.json
@@ -0,0 +1,15 @@
+{
+    "name": "batch",
+    "version": "0.5.1",
+    "description": "Simple async batch",
+    "author": "TJ Holowaychuk <tj at vision-media.ca>",
+    "devDependencies": {
+        "mocha": "*",
+        "should": "*"
+    },
+    "main": "index",
+    "repository": {
+        "type": "git",
+        "url": "https://github.com/visionmedia/batch.git"
+    }
+}
diff --git a/test/batch.js b/test/batch.js
new file mode 100644
index 0000000..af9e664
--- /dev/null
+++ b/test/batch.js
@@ -0,0 +1,151 @@
+
+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

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-batch.git



More information about the Pkg-javascript-commits mailing list