From e188e7789426e2b25f2044c80be109a272bdcb8b Mon Sep 17 00:00:00 2001 From: Olivier Dubuc Date: Wed, 18 Oct 2023 16:23:30 -0400 Subject: [PATCH] feat: support dequeue event to properly match enqueue counterpart --- lib/pool.js | 2 ++ promise.d.ts | 1 + promise.js | 2 +- test/integration/promise-wrappers/test-promise-wrappers.js | 7 ++++++- typings/mysql/lib/Pool.d.ts | 1 + 5 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/pool.js b/lib/pool.js index db54a7ab76..bc32be67d9 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -87,10 +87,12 @@ class Pool extends EventEmitter { // The connection has been removed from the pool and is no longer good. if (this._connectionQueue.length) { cb = this._connectionQueue.shift(); + this.emit('dequeue', connection); process.nextTick(this.getConnection.bind(this, cb)); } } else if (this._connectionQueue.length) { cb = this._connectionQueue.shift(); + this.emit('dequeue', connection); process.nextTick(cb.bind(null, null, connection)); } else { this._freeConnections.push(connection); diff --git a/promise.d.ts b/promise.d.ts index e9f3e08a1d..099ce564ee 100644 --- a/promise.d.ts +++ b/promise.d.ts @@ -90,6 +90,7 @@ export interface Pool extends Connection { on(event: 'acquire', listener: (connection: PoolConnection) => any): this; on(event: 'release', listener: (connection: PoolConnection) => any): this; on(event: 'enqueue', listener: () => any): this; + on(event: 'dequeue', listener: () => any): this; end(): Promise; diff --git a/promise.js b/promise.js index 95c5808b45..a407c1b020 100644 --- a/promise.js +++ b/promise.js @@ -331,7 +331,7 @@ class PromisePool extends EventEmitter { super(); this.pool = pool; this.Promise = thePromise || Promise; - inheritEvents(pool, this, ['acquire', 'connection', 'enqueue', 'release']); + inheritEvents(pool, this, ['acquire', 'connection', 'enqueue', 'release', 'dequeue']); } getConnection() { diff --git a/test/integration/promise-wrappers/test-promise-wrappers.js b/test/integration/promise-wrappers/test-promise-wrappers.js index 6a1fc17448..b860e43a7e 100644 --- a/test/integration/promise-wrappers/test-promise-wrappers.js +++ b/test/integration/promise-wrappers/test-promise-wrappers.js @@ -326,17 +326,22 @@ function testEventsPool() { assert.equal(this, pool); ++events; }) + .once('dequeue', function() { + assert.equal(this, pool); + ++events; + }) .once('release', function() { assert.equal(this, pool); ++events; - doneEventsPool = events === 4; + doneEventsPool = events === 5; }); /* eslint-enable no-invalid-this */ pool.pool.emit('acquire'); pool.pool.emit('connection'); pool.pool.emit('enqueue'); + pool.pool.emit('dequeue'); pool.pool.emit('release'); for (const eventName in expectedListeners) { diff --git a/typings/mysql/lib/Pool.d.ts b/typings/mysql/lib/Pool.d.ts index 93f8499561..d95b901a90 100644 --- a/typings/mysql/lib/Pool.d.ts +++ b/typings/mysql/lib/Pool.d.ts @@ -68,6 +68,7 @@ declare class Pool extends QueryableBase(ExecutableBase(EventEmitter)) { on(event: 'acquire', listener: (connection: PoolConnection) => any): this; on(event: 'release', listener: (connection: PoolConnection) => any): this; on(event: 'enqueue', listener: () => any): this; + on(event: 'dequeue', listener: () => any): this; unprepare(sql: string): PrepareStatementInfo;