mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
Add 'connect' event to pool (#7)
* Have pool emit 'connect' callback with client * Ensure pool emits client on connect event
This commit is contained in:
parent
7ef08fd861
commit
63caf7cd4c
2
index.js
2
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)
|
||||
|
||||
24
test/events.js
Normal file
24
test/events.js
Normal file
@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user