Skip to content

Commit ed2091a

Browse files
committed
Exports drain event when queue finishes processing (fixes #272 #271 #306)
Thank you @grncdr and @littlebee
1 parent c767f15 commit ed2091a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lib/Connection.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Connection.prototype.connect = function(cb) {
3131

3232
this._socket.on('error', this._handleNetworkError.bind(this));
3333
this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
34+
this._protocol.on('drain', this._handleProtocolDrain.bind(this));
3435
this._protocol.on('end', this._handleProtocolEnd.bind(this));
3536
}
3637

@@ -128,6 +129,10 @@ Connection.prototype._handleProtocolError = function(err) {
128129
this.emit('error', err);
129130
};
130131

132+
Connection.prototype._handleProtocolDrain = function(err) {
133+
this.emit('drain', err);
134+
};
135+
131136
Connection.prototype._handleProtocolEnd = function(err) {
132137
this.emit('end', err);
133138
};

lib/protocol/Protocol.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Protocol.prototype._dequeue = function() {
195195

196196
var sequence = this._queue[0];
197197
if (!sequence) {
198+
this.emit('drain');
198199
return;
199200
}
200201

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var common = require('../../common');
2+
var connection = common.createConnection();
3+
var assert = require('assert');
4+
5+
connection.connect();
6+
7+
var got_drain = false;
8+
9+
connection.on('drain', function() {
10+
got_drain = true;
11+
});
12+
13+
connection.query("SELECT 1", function(err) {
14+
assert.equal(got_drain, false);
15+
assert.ok(!err);
16+
process.nextTick(function() {
17+
assert.equal(got_drain, true);
18+
connection.end();
19+
});
20+
});

0 commit comments

Comments
 (0)