Skip to content

Commit

Permalink
[CONJS-258] All eventEmitters methods are not available on connections
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed May 31, 2023
1 parent c8c3b88 commit d61d3c6
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 7 deletions.
72 changes: 69 additions & 3 deletions lib/connection-callback.js
Expand Up @@ -10,9 +10,6 @@ class ConnectionCallback {

constructor(conn) {
this.#conn = conn;
this.on = this.#conn.on.bind(this.#conn);
this.once = this.#conn.once.bind(this.#conn);
this.listeners = this.#conn.listeners.bind(this.#conn);
}

get threadId() {
Expand Down Expand Up @@ -418,6 +415,75 @@ class ConnectionCallback {
break;
}
}

//*****************************************************************
// EventEmitter proxy methods
//*****************************************************************

on(eventName, listener) {
this.#conn.on.call(this.#conn, eventName, listener);
return this;
}

off(eventName, listener) {
this.#conn.off.call(this.#conn, eventName, listener);
return this;
}

once(eventName, listener) {
this.#conn.once.call(this.#conn, eventName, listener);
return this;
}

listeners(eventName) {
return this.#conn.listeners.call(this.#conn, eventName);
}

addListener(eventName, listener) {
this.#conn.addListener.call(this.#conn, eventName, listener);
return this;
}

eventNames() {
return this.#conn.eventNames.call(this.#conn);
}

getMaxListeners() {
return this.#conn.getMaxListeners.call(this.#conn);
}

listenerCount(eventName, listener) {
return this.#conn.listenerCount.call(this.#conn, eventName, listener);
}

prependListener(eventName, listener) {
this.#conn.prependListener.call(this.#conn, eventName, listener);
return this;
}

prependOnceListener(eventName, listener) {
this.#conn.prependOnceListener.call(this.#conn, eventName, listener);
return this;
}

removeAllListeners(eventName, listener) {
this.#conn.removeAllListeners.call(this.#conn, eventName, listener);
return this;
}

removeListener(eventName, listener) {
this.#conn.removeListener.call(this.#conn, eventName, listener);
return this;
}

setMaxListeners(n) {
this.#conn.setMaxListeners.call(this.#conn, n);
return this;
}

rawListeners(eventName) {
return this.#conn.rawListeners.call(this.#conn, eventName);
}
}

module.exports = ConnectionCallback;
72 changes: 69 additions & 3 deletions lib/connection-promise.js
Expand Up @@ -20,9 +20,6 @@ class ConnectionPromise {

constructor(conn) {
this.#conn = conn;
this.on = this.#conn.on.bind(this.#conn);
this.once = this.#conn.once.bind(this.#conn);
this.listeners = this.#conn.listeners.bind(this.#conn);
}

get threadId() {
Expand Down Expand Up @@ -294,6 +291,75 @@ class ConnectionPromise {
return this.#conn.escapeId(val);
}

//*****************************************************************
// EventEmitter proxy methods
//*****************************************************************

on(eventName, listener) {
this.#conn.on.call(this.#conn, eventName, listener);
return this;
}

off(eventName, listener) {
this.#conn.off.call(this.#conn, eventName, listener);
return this;
}

once(eventName, listener) {
this.#conn.once.call(this.#conn, eventName, listener);
return this;
}

listeners(eventName) {
return this.#conn.listeners.call(this.#conn, eventName);
}

addListener(eventName, listener) {
this.#conn.addListener.call(this.#conn, eventName, listener);
return this;
}

eventNames() {
return this.#conn.eventNames.call(this.#conn);
}

getMaxListeners() {
return this.#conn.getMaxListeners.call(this.#conn);
}

listenerCount(eventName, listener) {
return this.#conn.listenerCount.call(this.#conn, eventName, listener);
}

prependListener(eventName, listener) {
this.#conn.prependListener.call(this.#conn, eventName, listener);
return this;
}

prependOnceListener(eventName, listener) {
this.#conn.prependOnceListener.call(this.#conn, eventName, listener);
return this;
}

removeAllListeners(eventName, listener) {
this.#conn.removeAllListeners.call(this.#conn, eventName, listener);
return this;
}

removeListener(eventName, listener) {
this.#conn.removeListener.call(this.#conn, eventName, listener);
return this;
}

setMaxListeners(n) {
this.#conn.setMaxListeners.call(this.#conn, n);
return this;
}

rawListeners(eventName) {
return this.#conn.rawListeners.call(this.#conn, eventName);
}

//*****************************************************************
// internal public testing methods
//*****************************************************************
Expand Down
60 changes: 60 additions & 0 deletions test/integration/test-eventEmitter.js
@@ -0,0 +1,60 @@
'use strict';

const base = require('../base.js');
const { assert } = require('chai');

const basicListener = function basicListener(err) {
console.log(err);
};
describe('EventEmitter', () => {
it('event testing promise', async function () {
const conn = await base.createConnection();
testEvent(conn);
await conn.end();
});

it('event testing callback', function (done) {
const conn = base.createCallbackConnection();
conn.connect(() => {
testEvent(conn);
conn.end();
done();
});
});

function testEvent(conn) {
assert.equal(0, conn.listeners('error').length);
conn.on('error', basicListener);
assert.equal(1, conn.listeners('error').length);
conn.once('error', basicListener);
assert.equal(2, conn.listeners('error').length);
conn.off('error', basicListener);
assert.equal(1, conn.listeners('error').length);
conn.addListener('error', basicListener);
assert.equal(2, conn.listeners('error').length);
conn.addListener('other', basicListener);
assert.equal(1, conn.listeners('other').length);
conn.addListener('error', (err) => {});
assert.equal(3, conn.listeners('error').length);
// [ 'close_prepare', 'error', 'other' ]
assert.equal(3, conn.eventNames().length);
assert.isOk(conn.getMaxListeners() > 0);
assert.equal(3, conn.listenerCount('error'));
conn.prependListener('error', basicListener);
assert.equal(4, conn.listenerCount('error'));
conn.prependOnceListener('error', basicListener);
assert.equal(5, conn.listenerCount('error'));
assert.equal(1, conn.listenerCount('other'));
conn.removeListener('error', basicListener);
assert.equal(4, conn.listenerCount('error'));
assert.equal(4, conn.rawListeners('error').length);
conn.removeAllListeners('error');
assert.equal(0, conn.listenerCount('error'));
assert.equal(1, conn.listenerCount('other'));
conn.removeAllListeners('other');
assert.equal(0, conn.listenerCount('error'));
assert.equal(0, conn.listenerCount('other'));
conn.setMaxListeners(41);
assert.equal(41, conn.getMaxListeners());
}
});
4 changes: 3 additions & 1 deletion types/index.d.ts
Expand Up @@ -8,6 +8,7 @@
import tls = require('tls');
import stream = require('stream');
import geojson = require('geojson');
import events = require('events');

export const version: string;
export function createConnection(connectionUri: string | ConnectionConfig): Promise<Connection>;
Expand Down Expand Up @@ -594,7 +595,7 @@ export interface Prepare {
close(): void;
}

export interface Connection {
export interface Connection extends events.EventEmitter {
/**
* Connection information
*/
Expand Down Expand Up @@ -727,6 +728,7 @@ export interface Connection {

on(ev: 'end', callback: () => void): Connection;
on(ev: 'error', callback: (err: SqlError) => void): Connection;
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
listeners(ev: 'end'): (() => void)[];
listeners(ev: 'error'): ((err: SqlError) => void)[];
}
Expand Down
9 changes: 9 additions & 0 deletions types/mariadb-tests.ts
Expand Up @@ -176,6 +176,15 @@ async function testMisc(): Promise<void> {
})
.on('end', () => {
console.log(currRow + ' ' + metaReceived);
})
.once('end', () => {
console.log('t');
})
.once('release', () => {
console.log('t2');
})
.addListener('error', () => {
console.log('t2');
});
connection.listeners('end')[0]();
connection.listeners('error')[0](new SqlError('ddd'));
Expand Down

0 comments on commit d61d3c6

Please sign in to comment.