Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
autoconnect with optional callback
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Nov 14, 2013
1 parent 739e0dd commit 1996cca
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/deviceguider.js
Expand Up @@ -85,7 +85,7 @@ function DeviceGuider(deviceLoader) {
if (self.currentState.connectionMode !== 'manualconnect') {
if (self.currentState.connectionMode === 'autoconnect' || self.currentState.connectionMode === 'autoconnectOne' && self.currentState.connected.length === 0) {
if (!connecting) {
self.connectDevice(device, function() {
self.connect(device, function() {
connecting = false;
});
connecting = true;
Expand Down Expand Up @@ -198,13 +198,18 @@ DeviceGuider.prototype.getCurrentState = function(callback) {
* It emits 'connectionModeChanged'.
* When plugging a device it will now automatically connect it and emit 'connect'.
* Already plugged devices will connect immediately.
* @param {Boolean} directlyConnect If set, connections will be opened. [optional]
* @param {Boolean} directlyConnect If set, connections will be opened. [optional]
* @param {Function} callback The function, that will be called when function has finished. [optional]
* `function(err){}`
*/
DeviceGuider.prototype.autoconnect = function(directlyConnect) {
DeviceGuider.prototype.autoconnect = function(directlyConnect, callbacl) {
var self = this;

if (_.isFunction(directlyConnect)) {
directlyConnect = false;
if (arguments.length === 1) {
if (_.isFunction(directlyConnect)) {
callback = directlyConnect;
directlyConnect = false;
}
}

if (this.changeConnectionMode('autoconnect')) {
Expand All @@ -213,6 +218,7 @@ DeviceGuider.prototype.autoconnect = function(directlyConnect) {
}

if (!directlyConnect) {
if (callback) { callback(null); }
return;
}

Expand All @@ -222,8 +228,13 @@ DeviceGuider.prototype.autoconnect = function(directlyConnect) {
});
});

_.each(toConnect, function(dev) {
self.connectDevice(dev);
async.forEachSeries(toConnect, function(dc, clb) {
self.connect(dc);
}, function(err) {
if (err && !err.name) {
err = new Error(err);
}
if (callback) { return callback(err); }
});
}
};
Expand Down Expand Up @@ -294,7 +305,7 @@ DeviceGuider.prototype.autoconnectOne = function(directlyConnect, callback) {
}

if (self.currentState.plugged.length > 0 && self.currentState.connected.length === 0) {
self.connectDevice(self.currentState.plugged[0], callback);
self.connect(self.currentState.plugged[0], callback);
} else {
if (callback) callback(new Error('No device available!'));
}
Expand Down

0 comments on commit 1996cca

Please sign in to comment.