Skip to content
Browse files

tasks classes

  • Loading branch information...
1 parent 52e3eb7 commit 8f30786d5adf1f2ec38025511d23ac03cc588c99 @moshest moshest committed Nov 6, 2012
Showing with 223 additions and 0 deletions.
  1. +3 −0 index.js
  2. +119 −0 lib/parallel.js
  3. +101 −0 lib/serial.js
View
3 index.js
@@ -2,7 +2,10 @@ var further = module.exports;
further.registery = require('./lib/registery');
+further.Serial = require('./lib/serial');
+further.Parallel = require('./lib/parallel');
further.Controller = require('./lib/controller');
+
further.setup = require('./lib/setup');
further.requires = require('./lib/requires');
further.require = require('./lib/require');
View
119 lib/parallel.js
@@ -0,0 +1,119 @@
+
+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;
+};
View
101 lib/serial.js
@@ -0,0 +1,101 @@
+
+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 comments on commit 8f30786

Please sign in to comment.
Something went wrong with that request. Please try again.