Permalink
Browse files

examples git

tasks refactor
  • Loading branch information...
moshest committed Dec 6, 2012
1 parent 8f30786 commit de99baf6e8899ab8656fa4f28f53d58c2879a129
Showing with 306 additions and 222 deletions.
  1. +2 −2 index.js
  2. +0 −119 lib/parallel.js
  3. +0 −101 lib/serial.js
  4. +89 −0 lib/tasks/container.js
  5. +80 −0 lib/tasks/parallel.js
  6. +52 −0 lib/tasks/serial.js
  7. +77 −0 lib/tasks/task.js
  8. +6 −0 readme.md
View
@@ -2,8 +2,8 @@ var further = module.exports;
further.registery = require('./lib/registery');
-further.Serial = require('./lib/serial');
-further.Parallel = require('./lib/parallel');
+further.Serial = require('./lib/tasks/serial');
+further.Parallel = require('./lib/tasks/parallel');
further.Controller = require('./lib/controller');
further.setup = require('./lib/setup');
View
@@ -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;
-};
View
@@ -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);
- };
-};
View
@@ -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

Please sign in to comment.