Merge pull request #296 from hybridgroup/remove/async

Remove dependency on 'async'
This commit is contained in:
Edgar O Silva 2015-06-24 13:30:43 -05:00
commit fd564f61f8
5 changed files with 161 additions and 16 deletions

View File

@ -1,7 +1,5 @@
"use strict"; "use strict";
var Async = require("async");
var Logger = require("./logger"), var Logger = require("./logger"),
Robot = require("./robot"), Robot = require("./robot"),
Config = require("./config"), Config = require("./config"),
@ -105,7 +103,7 @@ Cylon.api = function api(Server, opts) {
Cylon.start = function start() { Cylon.start = function start() {
var starters = _.pluck(this.robots, "start"); var starters = _.pluck(this.robots, "start");
Async.parallel(starters, function() { _.parallel(starters, function() {
var mode = Utils.fetch(Config, "workMode", "async"); var mode = Utils.fetch(Config, "workMode", "async");
if (mode === "sync") { if (mode === "sync") {
@ -138,7 +136,7 @@ Cylon.halt = function halt(callback) {
var timeout = Config.haltTimeout || 3000; var timeout = Config.haltTimeout || 3000;
Utils.after(timeout, callback); Utils.after(timeout, callback);
Async.parallel(fns, callback); _.parallel(fns, callback);
}; };
Cylon.toJSON = function() { Cylon.toJSON = function() {

View File

@ -8,8 +8,7 @@ var initializer = require("./initializer"),
var validator = require("./validator"); var validator = require("./validator");
var Async = require("async"), var EventEmitter = require("events").EventEmitter;
EventEmitter = require("events").EventEmitter;
// used when creating default robot names // used when creating default robot names
var ROBOT_ID = 1; var ROBOT_ID = 1;
@ -280,7 +279,7 @@ Robot.prototype.start = function(callback) {
} }
}.bind(this); }.bind(this);
Async.series([ _.series([
this.startConnections, this.startConnections,
this.startDevices, this.startDevices,
start start
@ -348,7 +347,7 @@ Robot.prototype.startConnections = function(callback) {
}.bind(this); }.bind(this);
}, this); }, this);
return Async.parallel(starters, callback); return _.parallel(starters, callback);
}; };
/** /**
@ -378,7 +377,7 @@ Robot.prototype.startDevices = function(callback) {
}; };
}, this); }, this);
return Async.parallel(starters, callback); return _.parallel(starters, callback);
}; };
/** /**
@ -398,8 +397,8 @@ Robot.prototype.halt = function(callback) {
connections = _.pluck(this.connections, "disconnect"); connections = _.pluck(this.connections, "disconnect");
try { try {
Async.parallel(devices, function() { _.parallel(devices, function() {
Async.parallel(connections, callback); _.parallel(connections, callback);
}); });
} catch (e) { } catch (e) {
var msg = "An error occured while attempting to safely halt the robot"; var msg = "An error occured while attempting to safely halt the robot";

View File

@ -1,5 +1,7 @@
"use strict"; "use strict";
/* eslint no-use-before-define: 0 */
var __slice = Array.prototype.slice; var __slice = Array.prototype.slice;
var H = module.exports = {}; var H = module.exports = {};
@ -202,3 +204,68 @@ function includes(arr, value) {
extend(H, { extend(H, {
includes: includes includes: includes
}); });
function parallel(functions, done) {
var total = functions.length,
completed = 0,
results = [],
error;
function callback(err, result) {
if (error) {
return;
}
if (err || error) {
error = err;
done(err);
return;
}
completed++;
results.push(result);
if (completed === total) {
done(null, results);
}
}
if (!functions.length) { done(); }
functions.forEach(function(fn) { fn(callback); });
}
extend(H, {
parallel: parallel
});
function series(functions, done) {
var results = [],
error;
function callback(err, result) {
if (err || error) {
error = err;
return done(err);
}
results.push(result);
if (!functions.length) {
return done(null, results);
}
next();
}
function next() {
functions.shift()(callback);
}
if (!functions.length) { done(null, results); }
next();
}
extend(H, {
series: series
});

View File

@ -37,7 +37,6 @@
"hardware": { "hardware": {
"*": false, "*": false,
"./": false, "./": false,
"async": true,
"./lib": true "./lib": true
}, },
@ -51,9 +50,5 @@
"mocha": "2.2.4", "mocha": "2.2.4",
"sinon": "1.14.1", "sinon": "1.14.1",
"eslint": "0.22.1" "eslint": "0.22.1"
},
"dependencies": {
"async": "0.9.0"
} }
} }

View File

@ -289,4 +289,90 @@ describe("Helpers", function() {
expect(fn(arr, {})).to.be.eql(false); expect(fn(arr, {})).to.be.eql(false);
}); });
}); });
describe("#parallel", function() {
var fn1, fn2, fn3, callback;
beforeEach(function() {
fn1 = stub();
fn2 = stub();
fn3 = stub();
callback = stub();
});
it("executes a set of functions in parallel", function() {
_.parallel([fn1, fn2, fn3], callback);
expect(fn1).to.be.called;
expect(fn2).to.be.called;
expect(fn3).to.be.called;
expect(callback).to.not.be.called;
fn1.yield(null, true);
expect(callback).to.not.be.called;
fn2.yield(null, true);
expect(callback).to.not.be.called;
fn3.yield(null, true);
expect(callback).to.be.calledWith(null, [true, true, true]);
});
it("stops immediately if there's an error", function() {
_.parallel([fn1, fn2, fn3], callback);
fn1.yield(true, null);
expect(callback).to.be.calledWith(true);
fn2.yields(null, true);
fn3.yields(null, true);
expect(callback).to.be.calledOnce;
});
});
describe("#series", function() {
var fn1, fn2, fn3, callback;
beforeEach(function() {
fn1 = stub();
fn2 = stub();
fn3 = stub();
callback = stub();
});
it("executes a set of functions in series", function() {
_.series([fn1, fn2, fn3], callback);
expect(fn1).to.be.called;
expect(fn2).to.not.be.called;
expect(fn3).to.not.be.called;
expect(callback).to.not.be.called;
fn1.yield(null, true);
expect(fn2).to.be.called;
expect(fn3).to.not.be.called;
expect(callback).to.not.be.called;
fn2.yield(null, true);
expect(fn3).to.be.called;
expect(callback).to.not.be.called;
fn3.yield(null, true);
expect(callback).to.be.calledWith(null, [true, true, true]);
});
it("stops immediately if there's an error", function() {
_.series([fn1, fn2, fn3], callback);
fn1.yield(true, null);
expect(fn2).to.not.be.called;
expect(fn3).to.not.be.called;
expect(callback).to.be.calledWith(true);
});
});
}); });