From 5c9451d3ede24ec192b7af39462247d0d6208453 Mon Sep 17 00:00:00 2001 From: Ivan Tivonenko Date: Sat, 12 Sep 2015 22:18:52 +0300 Subject: [PATCH] allow Request.request(callback) --- src/core/request.js | 7 +++++-- src/core/server.js | 3 +-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/core/request.js b/src/core/request.js index 026ef216..a9f1c90d 100644 --- a/src/core/request.js +++ b/src/core/request.js @@ -39,8 +39,9 @@ function Request(remote, command) { util.inherits(Request, EventEmitter); // Send the request to a remote. -Request.prototype.request = function(servers, callback) { +Request.prototype.request = function(servers, callback_) { const self = this; + const callback = typeof servers === 'function' ? servers : callback_; this.emit('before'); this.callback(callback); @@ -247,7 +248,9 @@ Request.prototype.callback = function(callback, successEvent, errorEvent) { this.once(this.successEvent, requestSuccess); this.once(this.errorEvent, requestError); - this.request(); + if (!this.requested) { + this.request(); + } return this; }; diff --git a/src/core/server.js b/src/core/server.js index 14be988d..5d8a13b6 100644 --- a/src/core/server.js +++ b/src/core/server.js @@ -672,7 +672,6 @@ 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); @@ -687,8 +686,8 @@ Server.prototype._handleResponse = function(message) { error_message: 'Remote reported an error.', remote: message }); - request.emit('response'); } + request.emit('response', message); }; Server.prototype._handlePathFind = function(message) {