Permalink
Browse files

removed async dependency.

  • Loading branch information...
1 parent 69aeb13 commit e1d01af6bae5064b883a8ab58146502dfac51952 @kumatch committed May 7, 2012
Showing with 256 additions and 64 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 datagate.min.js
  3. +17 −10 lib/array.js
  4. +2 −2 lib/datagate.js
  5. +3 −3 lib/object.js
  6. +211 −0 lib/plow.js
  7. +16 −16 lib/union.js
  8. +1 −28 lib/utils.js
  9. +0 −1 lib/validator.js
  10. +4 −3 package.json
View
@@ -0,0 +1 @@
+node_modules
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -2,42 +2,49 @@ var utils = require('./utils');
var ArrayError = require('./error').ArrayError;
var _ = utils._;
-var async = utils.async;
+var plow = utils.plow;
module.exports = function createNewArrayGate(gate, error_message) {
if (error_message === undefined) {
error_message = 'Invalid value in array.';
}
return function (values, callback) {
- var results = [];
var errors = [];
+ var results = [];
if (!_.isArray(values)) {
callback(new ArrayError({
message: error_message,
origin: values,
- result: results,
+ result: [],
errors: errors
- }), results);
+ }), []);
+
return;
}
-
- async.forEachSeries(values, function (value, next) {
+ plow.map(values, function (value, index, next) {
if (typeof gate === 'function') {
gate(value, function (err, result) {
- results.push(result);
if (err) {
- errors.push(err);
+ errors[index] = err;
}
- next();
+ results[index] = result;
+ next(null);
});
} else {
- results.push(value);
+ results[index] = value;
next();
}
}, function (err) {
+ if (err) {
+ callback(err);
+ return;
+ }
+
+ errors = _.compact(errors);
+
if (errors.length) {
var total_err = new ArrayError({
message: error_message,
View
@@ -2,7 +2,7 @@ var utils = require('./utils');
var VariableError = require('./error').VariableError;
var _ = utils._;
-var async = utils.async;
+var plow = utils.plow;
exports = module.exports = datagate;
@@ -64,7 +64,7 @@ function datagate(entries, root_error_message) {
next(null, value, value);
});
- async.waterfall(tasks, function (err, origin, result) {
+ plow.sequential(tasks, function (err, origin, result) {
if (!err) {
callback(null, result);
} else {
View
@@ -2,7 +2,7 @@ var utils = require('./utils');
var ObjectError = require('./error').ObjectError;
var _ = utils._;
-var async = utils.async;
+var plow = utils.plow;
module.exports = function createNewObjectGate(entries, error_message) {
if (error_message === undefined) {
@@ -21,8 +21,8 @@ module.exports = function createNewObjectGate(entries, error_message) {
has_error = true;
}
- async.forEach(properties, function (property, next) {
- var gate = entries[property];
+ plow.map(properties, function (property, next) {
+ var gate = entries[property];
var value = object ? object[property] : undefined;
gate(value, function (err, result) {
View
@@ -0,0 +1,211 @@
+// plow.js 0.1.1
+// (c) 2012 Yosuke Kumakura
+// https://github.com/kumatch/plow
+//
+// Licensed under the MIT license:
+// http://www.opensource.org/licenses/MIT
+
+exports.sequential = _sequential;
+exports.parallel = _parallel;
+exports.map = _map;
+exports.nextTick = _nextTick;
+
+var DEFAULT_CONCURRENCY = 10;
+
+function _sequential(list, end) {
+ var tasks = Array.prototype.slice.call(list);
+
+ end = end || function () {};
+
+ if (!tasks.length) {
+ end();
+ return;
+ }
+
+ (function next(err) {
+ if (err) {
+ end(err);
+ } else {
+ var task = tasks.shift();
+ var args = Array.prototype.slice.call(arguments);
+
+ if (tasks.length > 0) {
+ args = args.slice(1).concat(next);
+ } else {
+ args = args.slice(1).concat(end);
+ }
+
+ exports.nextTick(function () {
+ try {
+ task.apply(null, args);
+ } catch (e) {
+ end(e);
+ }
+ });
+ }
+ })(null);
+}
+
+
+
+function _parallel(tasks, end) {
+ var completed = 0;
+ var failed = false;
+ var is_array = _isArray(tasks);
+ var results, length;
+
+ if (is_array) {
+ results = [];
+ length = tasks.length;
+ } else {
+ results = {};
+ length = _keys(tasks).length;
+ }
+
+ end = end || function () {};
+
+ if (!length) {
+ end(null, results);
+ return;
+ }
+
+
+
+ var worker = function (task, index, next) {
+ task(function (err, value) {
+ if (err) {
+ next(err);
+ } else {
+ next(null, {
+ index: index,
+ value: value
+ });
+ }
+ });
+ };
+
+ var concurrency = 1;
+ var index = 0;
+
+ _doWork(concurrency, tasks, worker, index, function(err, result) {
+
+ if (err) {
+ end(err);
+ return false;
+ } else {
+ completed += 1;
+ results[result.index] = result.value;
+
+ if (completed === length) {
+ end(null, results);
+ }
+ return true;
+ }
+ });
+}
+
+
+function _map(list, worker, end) {
+ var length = list.length;
+ var results = [];
+ var failed = false;
+
+ end = end || function () {};
+
+ if (!length) {
+ end(null, []);
+ } else {
+ _doWork(DEFAULT_CONCURRENCY, list, worker, 0, function(err, result) {
+ if (err) {
+ end(err, results);
+ return false;
+ } else {
+ results.push(result);
+
+ if (results.length === length) {
+ end(null, results);
+ }
+ return true;
+ }
+ });
+ }
+}
+
+
+function _doWork(concurrency, list, worker, currentIndex, callback) {
+ var failed = false;
+ var is_array = _isArray(list);
+ var keys, length;
+
+ if (is_array) {
+ length = list.length;
+ } else {
+ keys = _keys(list);
+ length = keys.length;
+ }
+
+ function workerCallback (err, result) {
+ if (!callback(err, result)) {
+ failed = true;
+ }
+ }
+
+ exports.nextTick(function () {
+ for (var i = currentIndex; (i < length && i < (currentIndex + concurrency)); i++) {
+ if (!failed) {
+ try {
+ var value, index;
+
+ if (is_array) {
+ index = i;
+ value = list[index];
+ } else {
+ index = keys[i];
+ value = list[index];
+ }
+
+ if (worker.length > 2) {
+ worker(value, index, workerCallback);
+ } else {
+ worker(value, workerCallback);
+ }
+ } catch (e) {
+ failed = true;
+ callback(e);
+ }
+ }
+ }
+
+ if (!failed && i < length) {
+ _doWork(concurrency, list, worker, i, callback);
+ }
+ });
+}
+
+
+function _nextTick(func) {
+ if (typeof process === 'undefined' || !(process.nextTick)) {
+ setTimeout(func, 0);
+ } else {
+ process.nextTick(func);
+ }
+}
+
+
+function _isArray(array) {
+ return (Object.prototype.toString.call(array) === '[object Array]');
+}
+
+function _keys(obj) {
+ if (typeof Object.keys !== 'function') {
+ return Object.keys(obj);
+ }
+
+ var keys = [];
+ for (var k in obj) {
+ if (obj.hasOwnProperty(k)) {
+ keys.push(k);
+ }
+ }
+ return keys;
+}
View
@@ -1,4 +1,4 @@
-var async = require('./utils').async;
+var plow = require('./utils').plow;
var UnionError = require('./error').UnionError;
module.exports = function createNewUnionGate(gates, error_message) {
@@ -14,7 +14,7 @@ module.exports = function createNewUnionGate(gates, error_message) {
var errors = [];
var last_result;
- async.forEachSeries(gates, function (gate, next) {
+ plow.map(gates, function (gate, next) {
gate(value, function (err, result) {
last_result = result;
@@ -23,21 +23,21 @@ module.exports = function createNewUnionGate(gates, error_message) {
next();
} else {
callback(null, result);
- }
- });
- }, function (err) {
- if (err) {
- callback(err);
- } else {
- var union_err = new UnionError({
- message: error_message,
- origin: value,
- result: last_result,
- errors: errors
- });
-
- callback(union_err, last_result);
}
});
+ }, function (err) {
+ if (err) {
+ callback(err);
+ } else {
+ var union_err = new UnionError({
+ message: error_message,
+ origin: value,
+ result: last_result,
+ errors: errors
+ });
+
+ callback(union_err, last_result);
+ }
+ });
};
};
View
@@ -4,31 +4,4 @@ if (typeof _ !== 'undefined' && _.VERSION) {
exports._ = require('underscore');
}
-if (typeof async !== 'undefined') {
- exports.async = async;
-} else {
- exports.async = require('async');
-}
-
-
-exports.chain = function (acts, last) {
- next();
-
- function next(err) {
- if (err) {
- // acts.pop()(err);
- last(err);
- } else {
- var act = acts.shift();
- var args = Array.prototype.slice.call(arguments);
-
- if (acts.length > 0) {
- args = args.slice(1).concat(next);
- } else {
- args = args.slice(1).concat(last);
- }
-
- act.apply(null, args);
- }
- }
-};
+exports.plow = require('./plow');
Oops, something went wrong.

0 comments on commit e1d01af

Please sign in to comment.