Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dequeue event to properly match enqueue counterpart #2250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading