make sure that 'before' event emitted from Request only once

This commit is contained in:
Ivan Tivonenko
2015-09-28 03:00:59 +03:00
parent 87fdbc932f
commit ed3b04ed6f
2 changed files with 22 additions and 2 deletions

View File

@@ -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);

View File

@@ -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();
});
}); });