Skip to content

Commit

Permalink
move reconnect logic from Request.callback to Request.request
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdarkdragon committed Sep 9, 2015
1 parent c57cef4 commit 7706866
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ util.inherits(Request, EventEmitter);

// Send the request to a remote.
Request.prototype.request = function(servers, callback) {
const self = this;

this.emit('before');
this.callback(callback);

Expand All @@ -51,15 +53,32 @@ Request.prototype.request = function(servers, callback) {
this.on('error', function() {});
this.emit('request', this.remote);

if (Array.isArray(servers)) {
servers.forEach(function(server) {
this.setServer(server);
this.remote.request(this);
}, this);
} else {
this.remote.request(this);
function doRequest() {
if (Array.isArray(servers)) {
servers.forEach(function(server) {
self.setServer(server);
self.remote.request(self);
}, self);
} else {
self.remote.request(self);
}
}

function onReconnect() {
doRequest();
}

function onResponse() {
self.remote.removeListener('connected', onReconnect);
}

if (this.remote.isConnected()) {
this.remote.on('connected', onReconnect);
}
this.once('response', onResponse);

doRequest();

return this;
};

Expand Down Expand Up @@ -207,20 +226,14 @@ Request.prototype.callback = function(callback, successEvent, errorEvent) {

let called = false;

function onReconnect() {
self.remote.request(self);
}

function requestSuccess(message) {
self.remote.removeListener('connected', onReconnect);
if (!called) {
called = true;
callback.call(self, null, message);
}
}

function requestError(error) {
self.remote.removeListener('connected', onReconnect);
if (!called) {
called = true;

Expand All @@ -234,9 +247,6 @@ Request.prototype.callback = function(callback, successEvent, errorEvent) {

this.once(this.successEvent, requestSuccess);
this.once(this.errorEvent, requestError);
if (this.remote.isConnected()) {
this.remote.once('connected', onReconnect);
}
this.request();

return this;
Expand Down
2 changes: 2 additions & 0 deletions src/core/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ Server.prototype._handleResponse = function(message) {
const responseEvent = 'response_' + command;

request.emit('success', result);
request.emit('response', result);

[this, this._remote].forEach(function(emitter) {
emitter.emit(responseEvent, result, request, message);
Expand All @@ -686,6 +687,7 @@ Server.prototype._handleResponse = function(message) {
error_message: 'Remote reported an error.',
remote: message
});
request.emit('response');
}
};

Expand Down
5 changes: 5 additions & 0 deletions test/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ describe('Request', function() {
assert.strictEqual(typeof req.message, 'object');
assert.strictEqual(req.message.command, 'server_info');
done();
},
on: function() {
},
isConnected: function() {
return true;
}
};

Expand Down

0 comments on commit 7706866

Please sign in to comment.