Skip to content

Commit 63caf7c

Browse files
authored
Add 'connect' event to pool (#7)
* Have pool emit 'connect' callback with client * Ensure pool emits client on connect event
1 parent 7ef08fd commit 63caf7c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var Pool = module.exports = function (options, Client) {
1414
this.options.create = this.options.create || this._create.bind(this)
1515
this.options.destroy = this.options.destroy || this._destroy.bind(this)
1616
this.pool = new genericPool.Pool(this.options)
17+
this.onCreate = this.options.onCreate
1718
}
1819

1920
util.inherits(Pool, EventEmitter)
@@ -37,6 +38,7 @@ Pool.prototype._create = function (cb) {
3738

3839
client.connect(function (err) {
3940
this.log('client connected')
41+
this.emit('connect', client)
4042
if (err) {
4143
this.log('client connection error:', err)
4244
cb(err)

test/events.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var expect = require('expect.js')
2+
3+
var describe = require('mocha').describe
4+
var it = require('mocha').it
5+
6+
var Pool = require('../')
7+
8+
describe('events', function () {
9+
it('emits connect before callback', function (done) {
10+
var pool = new Pool()
11+
var emittedClient = false
12+
pool.on('connect', function (client) {
13+
emittedClient = client
14+
})
15+
16+
pool.connect(function (err, client, release) {
17+
if (err) return done(err)
18+
release()
19+
pool.end()
20+
expect(client).to.be(emittedClient)
21+
done()
22+
})
23+
})
24+
})

0 commit comments

Comments
 (0)