Permalink
Please sign in to comment.
Showing
with
306 additions
and 222 deletions.
- +2 −2 index.js
- +0 −119 lib/parallel.js
- +0 −101 lib/serial.js
- +89 −0 lib/tasks/container.js
- +80 −0 lib/tasks/parallel.js
- +52 −0 lib/tasks/serial.js
- +77 −0 lib/tasks/task.js
- +6 −0 readme.md
119
lib/parallel.js
@@ -1,119 +0,0 @@ | ||
- | ||
-module.exports = Parallel; | ||
- | ||
-var push = function(self, callback, key) { | ||
- var obj = { | ||
- pos: self._callbacks.length, | ||
- key: key, | ||
- callback: callback, | ||
- called: false, | ||
- result: null | ||
- }; | ||
- | ||
- self._callbacks.push(obj); | ||
- | ||
- if(key && !self._isMap) | ||
- self._isMap = true; | ||
- | ||
- // has error, nothing todo any more | ||
- if(self._err) | ||
- return; | ||
- | ||
- self._refCount++; | ||
- callback.call(self, function(err) { | ||
- if(obj.called) { | ||
- err = new Error('Callback called more then once'); | ||
- } else { | ||
- obj.called = true; | ||
- self._refCount--; | ||
- } | ||
- | ||
- if(err) { | ||
- self._err = err; | ||
- | ||
- self.run(); | ||
- return; | ||
- } | ||
- | ||
- obj.result = arguments.length <= 2 | ||
- ? arguments[0] | ||
- : Array.prototype.slice.call(arguments, 1); | ||
- | ||
- if(!self._refCount) | ||
- self.run(); | ||
- }); | ||
-}; | ||
- | ||
-function Parallel() { | ||
- this._err = null; | ||
- this._refCount = 0; | ||
- this._runs = []; | ||
- | ||
- this._callbacks = []; | ||
- this._isMap = false; | ||
-}; | ||
- | ||
-Parallel.prototype.push = function(key, callback) { | ||
- var self = this; | ||
- | ||
- if(typeof key === 'function') { | ||
- | ||
- for(var i = 0; i < arguments.length; i++) { | ||
- push(self, arguments[i]); | ||
- } | ||
- | ||
- } else if(typeof key === 'string') { | ||
- | ||
- push(self, callback, key); | ||
- | ||
- } else if(typeof key === 'object') { | ||
- | ||
- Object.keys(key).forEach(function(k) { | ||
- push(self, key[k], k); | ||
- }); | ||
- | ||
- } | ||
- | ||
- return this; | ||
-}; | ||
- | ||
-Serial.prototype.next = function(key) { | ||
- var self = this, | ||
- next = null; | ||
- | ||
- push(self, function(callback) { | ||
- next = callback; | ||
- }, key); | ||
- | ||
- return next; | ||
-}; | ||
- | ||
-Serial.prototype.run = function(callback) { | ||
- var self = this; | ||
- | ||
- if(callback) { | ||
- self._runs.push(callback); | ||
- } | ||
- | ||
- if(self._refCount && !self._err) | ||
- return this; | ||
- | ||
- var fns = self._runs, | ||
- results = !self._isMap ? [] : {}; | ||
- self._runs = []; | ||
- | ||
- if(!self._err) { | ||
- self._callbacks.forEach(function(obj) { | ||
- if(obj.key) | ||
- results[obj.key] = obj.result; | ||
- else | ||
- results[obj.pos] = obj.result; | ||
- }); | ||
- } | ||
- | ||
- fns.forEach(function(fn) { | ||
- fn.call(self, self._err, results); | ||
- }); | ||
- | ||
- return this; | ||
-}; |
101
lib/serial.js
@@ -1,101 +0,0 @@ | ||
- | ||
-module.exports = Serial; | ||
- | ||
-var push = function(self, callback, key) { | ||
- var obj = { | ||
- pos: self._callbacks.length, | ||
- key: key, | ||
- callback: callback, | ||
- called: false | ||
- }; | ||
- | ||
- self._callbacks.push(obj); | ||
- | ||
- if(key && !self._isMap) | ||
- self._isMap = true; | ||
-}; | ||
- | ||
-function Serial() { | ||
- this._callbacks = []; | ||
- this._isMap = false; | ||
-}; | ||
- | ||
-Serial.prototype.push = function(key, callback) { | ||
- var self = this; | ||
- | ||
- if(typeof key === 'function') { | ||
- | ||
- for(var i = 0; i < arguments.length; i++) { | ||
- push(self, arguments[i]); | ||
- } | ||
- | ||
- } else if(typeof key === 'string') { | ||
- | ||
- push(self, callback, key); | ||
- | ||
- } else if(typeof key === 'object') { | ||
- | ||
- Object.keys(key).forEach(function(k) { | ||
- push(self, key[k], k); | ||
- }); | ||
- | ||
- } | ||
- | ||
- return this; | ||
-}; | ||
- | ||
-Serial.prototype.next = function(key) { | ||
- var self = this, | ||
- next = function(err) { | ||
- next = [this, arguments]; | ||
- }; | ||
- | ||
- push(self, function(callback) { | ||
- // callback not called yet | ||
- if(typeof next === 'function') | ||
- next = callback; | ||
- else | ||
- callback.apply(next[0], next[1]); | ||
- }, key); | ||
- | ||
- return next; | ||
-}; | ||
- | ||
-Serial.prototype.run = function(callback) { | ||
- var self = this, | ||
- pos = 0, | ||
- results = !this._isMap ? [] : {}, | ||
- lastResult = null, | ||
- next = function() { | ||
- if(pos >= self._callbacks.length) { | ||
- callback(null, results); | ||
- return; | ||
- } | ||
- | ||
- var obj = self._callbacks[pos++]; | ||
- | ||
- obj.callback.call(self, function(err) { | ||
- if(obj.called) { | ||
- err = new Error('Callback called more then once'); | ||
- } else { | ||
- obj.called = true; | ||
- } | ||
- | ||
- if(err) { | ||
- callback(err); | ||
- return; | ||
- } | ||
- | ||
- lastResult = arguments.length <= 2 | ||
- ? arguments[0] | ||
- : Array.prototype.slice.call(arguments, 1); | ||
- | ||
- if(obj.key) | ||
- results[obj.key] = lastResult; | ||
- else | ||
- results[obj.pos] = lastResult; | ||
- | ||
- next(); | ||
- }, lastResult); | ||
- }; | ||
-}; |
@@ -0,0 +1,89 @@ | ||
+var util = require("util"), | ||
+ Task = require("./task"); | ||
+ | ||
+module.exports = Container; | ||
+ | ||
+var push = function(self, callback, key) { | ||
+ var obj = { | ||
+ pos: self._callbacks.length, | ||
+ key: key, | ||
+ callback: callback, | ||
+ called: false | ||
+ }; | ||
+ | ||
+ self._callbacks.push(obj); | ||
+ | ||
+ if(key && !self._isMap) | ||
+ self._isMap = true; | ||
+}; | ||
+ | ||
+ | ||
+function Container() { | ||
+ Container.super_.apply(this, arguments); | ||
+ | ||
+ this._callbacks = []; | ||
+ this._isMap = false; | ||
+}; | ||
+util.inherits(Container, Task); | ||
+ | ||
+ | ||
+Container.prototype.push = function(key, callback) { | ||
+ if((this._onDone !== null && !this._onDone.length)) { | ||
+ throw new Error('Task already ran'); | ||
+ } | ||
+ | ||
+ var self = this; | ||
+ | ||
+ if(typeof key === 'function') { | ||
+ | ||
+ for(var i = 0; i < arguments.length; i++) { | ||
+ push(self, arguments[i]); | ||
+ } | ||
+ | ||
+ } else if(typeof key === 'string') { | ||
+ | ||
+ push(self, callback, key); | ||
+ | ||
+ } else if(typeof key === 'object') { | ||
+ | ||
+ Object.keys(key).forEach(function(k) { | ||
+ push(self, key[k], k); | ||
+ }); | ||
+ | ||
+ } | ||
+ | ||
+ return this; | ||
+}; | ||
+ | ||
+ | ||
+Container.prototype.next = function(key, returns) { | ||
+ var cb = null, | ||
+ next = function() { | ||
+ if(typeof cb === 'function') { | ||
+ // callback already called | ||
+ cb.apply(this, arguments); | ||
+ } else { | ||
+ // update cb for callback | ||
+ cb = [this, arguments]; | ||
+ } | ||
+ | ||
+ return returns; | ||
+ }, | ||
+ | ||
+ fn = function(callback) { | ||
+ if(cb !== null) { | ||
+ // next already called | ||
+ callback.apply(cb[0], cb[1]); | ||
+ } else { | ||
+ // update cb for next | ||
+ cb = callback; | ||
+ } | ||
+ }; | ||
+ | ||
+ if(typeof key !== 'string') | ||
+ this.push(fn); | ||
+ else | ||
+ this.push(key, fn); | ||
+ | ||
+ return next; | ||
+}; |

Oops, something went wrong.
0 comments on commit
de99baf