Always call Remote.disconnect callback

This commit is contained in:
wltsmrz
2014-07-21 22:25:43 -07:00
parent cb59f86d4c
commit aea75f2beb
2 changed files with 20 additions and 6 deletions

View File

@@ -574,12 +574,17 @@ Remote.prototype.disconnect = function(callback) {
throw new Error('No servers available, not disconnecting');
}
if (typeof callback === 'function') {
this.once('disconnect', callback);
var callback = (typeof callback === 'function') ? callback : function(){};
if (!this._connected) {
callback();
return this;
}
this._should_connect = false;
this.once('disconnect', callback);
this._servers.forEach(function(server) {
server.disconnect();
});
@@ -773,6 +778,16 @@ Remote.prototype.setPrimaryServer = function(server) {
this._primary_server._primary = true;
};
/**
* Get connected state
*
* @return {Boolean} connected
*/
Remote.prototype.isConnected = function() {
return this._connected;
};
/**
* Select a server to handle a request. Servers are
* automatically prioritized
@@ -782,7 +797,7 @@ Remote.prototype._getServer =
Remote.prototype.getServer = function() {
var result = void(0);
if (this._primary_server && this._primary_server._connected) {
if (this._primary_server && this._primary_server.isConnected()) {
return this._primary_server;
}

View File

@@ -316,7 +316,6 @@ Server.prototype.disconnect = function() {
this._lastLedgerIndex = NaN;
this._lastLedgerClose = NaN;
this._score = 0;
this._shouldConnect = false;
this._setState('offline');
@@ -341,11 +340,11 @@ Server.prototype.reconnect = function() {
};
if (this._ws && this._shouldConnect) {
if (this._connected) {
if (this.isConnected()) {
this.once('disconnect', reconnect);
this.disconnect();
} else {
reconnect();
reconnect();
}
}
};