Skip to content

Commit

Permalink
feat: support dequeue event to properly match enqueue counterpart
Browse files Browse the repository at this point in the history
  • Loading branch information
odubuc committed Oct 18, 2023
1 parent fa47d0b commit e188e77
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions promise.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;

Expand Down
2 changes: 1 addition & 1 deletion promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 6 additions & 1 deletion test/integration/promise-wrappers/test-promise-wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions typings/mysql/lib/Pool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit e188e77

Please sign in to comment.