Skip to content

Add 'drain' event to Protocol and Connection. #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function Connection(options) {
this._socket = options.socket;
this._protocol = new Protocol({config: this.config});
this._connectCalled = false;
this._protocol.on('drain', this.emit.bind(this, 'drain'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you attach this callback inside the Connection#connect method? As a general rule, my constructors are kept as dump as possible, and this is where all the other Protocol event handlers are defined.

}

Connection.prototype.connect = function(cb) {
Expand Down
1 change: 1 addition & 0 deletions lib/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ Protocol.prototype._dequeue = function() {

var sequence = this._queue[0];
if (!sequence) {
this.emit('drain')
return;
}

Expand Down
22 changes: 22 additions & 0 deletions test/integration/connection/test-drain-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var common = require('../../common');
var connection = common.createConnection();
var assert = require('assert');

connection.connect();

var gotDrain = false;

connection.on('drain', function() {
gotDrain = true;
});

connection.query("SELECT 1", function(err) {
// drain is not emitted until after the callback completes
assert.equal(gotDrain, false);
assert.ok(!err);
process.nextTick(function() {
assert.equal(gotDrain, true);
connection.end()
})
})