Permalink
Browse files

add parallel example

  • Loading branch information...
tj committed Mar 13, 2013
1 parent e163fc6 commit 610c380b66181bc74570b034b046027c95f5d726
Showing with 114 additions and 91 deletions.
  1. 0 examples/concurrency.js
  2. +23 −0 examples/parallel.js
  3. 0 examples/serial.js
  4. +91 −1 index.js
  5. +0 −90 lib/batch.js
View
No changes.
View
@@ -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();
View
No changes.
View
@@ -1,2 +1,92 @@
-module.exports = require('./lib/batch');
+/**
+ * Module dependencies.
+ */
+
+var EventEmitter = require('events').EventEmitter
+ , noop = function(){};
+
+/**
+ * Expose `Batch`.
+ */
+
+module.exports = Batch;
+
+/**
+ * Create a new Batch.
+ */
+
+function Batch() {
+ EventEmitter.call(this);
+ this.fns = [];
+ for (var i = 0, len = arguments.length; i < len; ++i) {
+ this.push(arguments[i]);
+ }
+}
+
+/**
+ * Inherit from `EventEmitter.prototype`.
+ */
+
+Batch.prototype.__proto__ = EventEmitter.prototype;
+
+/**
+ * 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 self = this
+ , total = this.fns.length
+ , pending = total
+ , results = []
+ , cb = cb || noop
+ , done;
+
+ if (!this.fns.length) return cb(null, results);
+
+ this.fns.forEach(function(fn, i){
+ var start = new Date;
+ fn(function(err, res){
+ if (done) return;
+ if (err) return done = true, cb(err);
+ var complete = total - pending + 1;
+ var end = new Date;
+
+ results[i] = res;
+
+ self.emit('progress', {
+ index: i,
+ value: res,
+ pending: pending,
+ total: total,
+ complete: complete,
+ percent: complete / total * 100 | 0,
+ start: start,
+ end: end,
+ duration: end - start
+ });
+
+ --pending || cb(null, results);
+ });
+ });
+
+ return this;
+};
View
@@ -1,90 +0,0 @@
-
-/**
- * Module dependencies.
- */
-
-var EventEmitter = require('events').EventEmitter;
-
-/**
- * Expose `Batch`.
- */
-
-module.exports = Batch;
-
-/**
- * Create a new Batch.
- */
-
-function Batch() {
- EventEmitter.call(this);
- this.fns = [];
- for (var i = 0, len = arguments.length; i < len; ++i) {
- this.push(arguments[i]);
- }
-}
-
-/**
- * Inherit from `EventEmitter.prototype`.
- */
-
-Batch.prototype.__proto__ = EventEmitter.prototype;
-
-/**
- * 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 self = this
- , total = this.fns.length
- , pending = total
- , results = []
- , done;
-
- if (!this.fns.length) return cb(null, results);
-
- this.fns.forEach(function(fn, i){
- var start = new Date;
- fn(function(err, res){
- if (done) return;
- if (err) return done = true, cb(err);
- var complete = total - pending + 1;
- var end = new Date;
-
- results[i] = res;
-
- self.emit('progress', {
- index: i,
- value: res,
- pending: pending,
- total: total,
- complete: complete,
- percent: complete / total * 100 | 0,
- start: start,
- end: end,
- duration: end - start
- });
-
- --pending || cb(null, results);
- });
- });
-
- return this;
-};

0 comments on commit 610c380

Please sign in to comment.