mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
make sure that 'before' event emitted from Request only once
This commit is contained in:
@@ -44,13 +44,15 @@ Request.prototype.request = function(servers, callback_) {
|
|||||||
const callback = typeof servers === 'function' ? servers : callback_;
|
const callback = typeof servers === 'function' ? servers : callback_;
|
||||||
|
|
||||||
this.emit('before');
|
this.emit('before');
|
||||||
|
|
||||||
|
const wasRequested = this.requested;
|
||||||
|
this.requested = true;
|
||||||
this.callback(callback);
|
this.callback(callback);
|
||||||
|
|
||||||
if (this.requested) {
|
if (wasRequested) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.requested = true;
|
|
||||||
this.on('error', function() {});
|
this.on('error', function() {});
|
||||||
this.emit('request', this.remote);
|
this.emit('request', this.remote);
|
||||||
|
|
||||||
|
|||||||
@@ -1207,4 +1207,22 @@ describe('Request', function() {
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Emit "before" only once', function(done) {
|
||||||
|
const remote = new Remote();
|
||||||
|
remote._connected = true;
|
||||||
|
|
||||||
|
const request = new Request(remote, 'server_info');
|
||||||
|
|
||||||
|
let beforeCalled = 0;
|
||||||
|
|
||||||
|
request.on('before', () => {
|
||||||
|
beforeCalled++;
|
||||||
|
});
|
||||||
|
|
||||||
|
request.request(function() {});
|
||||||
|
assert.strictEqual(beforeCalled, 1);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user