From 63caf7cd4c2255537b063274a1142b9a52148a4b Mon Sep 17 00:00:00 2001 From: Brian C Date: Wed, 22 Jun 2016 23:29:35 -0500 Subject: [PATCH] Add 'connect' event to pool (#7) * Have pool emit 'connect' callback with client * Ensure pool emits client on connect event --- index.js | 2 ++ test/events.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/events.js diff --git a/index.js b/index.js index 687e410f..ee09dc81 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ var Pool = module.exports = function (options, Client) { this.options.create = this.options.create || this._create.bind(this) this.options.destroy = this.options.destroy || this._destroy.bind(this) this.pool = new genericPool.Pool(this.options) + this.onCreate = this.options.onCreate } util.inherits(Pool, EventEmitter) @@ -37,6 +38,7 @@ Pool.prototype._create = function (cb) { client.connect(function (err) { this.log('client connected') + this.emit('connect', client) if (err) { this.log('client connection error:', err) cb(err) diff --git a/test/events.js b/test/events.js new file mode 100644 index 00000000..3b7225b7 --- /dev/null +++ b/test/events.js @@ -0,0 +1,24 @@ +var expect = require('expect.js') + +var describe = require('mocha').describe +var it = require('mocha').it + +var Pool = require('../') + +describe('events', function () { + it('emits connect before callback', function (done) { + var pool = new Pool() + var emittedClient = false + pool.on('connect', function (client) { + emittedClient = client + }) + + pool.connect(function (err, client, release) { + if (err) return done(err) + release() + pool.end() + expect(client).to.be(emittedClient) + done() + }) + }) +})