Skip to content

Commit b27e440

Browse files
committed
feat!: make ServerlessClient EventEmitter
1 parent 531ea3c commit b27e440

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import EventEmitter = require("events");
12
import stream = require("stream");
3+
import pg = require("pg");
24

35
declare interface TlsOptions {
46
rejectUnauthorized?: boolean;
@@ -75,7 +77,7 @@ declare namespace ServerlessClient {
7577
export { TlsOptions, Config };
7678
}
7779

78-
declare class ServerlessClient {
80+
declare class ServerlessClient extends EventEmitter {
7981
constructor(config: Config)
8082

8183
clean(): Promise<number | undefined>
@@ -88,7 +90,7 @@ declare class ServerlessClient {
8890

8991
end(): Promise<any>
9092

91-
on(...args): void
93+
on(event: "init", listener: (client: pg.Client) => void): this
9294
}
9395

9496
export = ServerlessClient

package-lock.json

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
* @license MIT
77
*/
88

9+
const EventEmitter = require("events");
910
const {isValidStrategy, type, validateNum, isWithinRange} = require("./utils");
1011
const Postgres = require("./postgres");
1112

1213
function ServerlessClient(config) {
14+
EventEmitter.call(this);
15+
1316
this._client = null;
1417
if (config.plugin) {
1518
this._plugin = config.plugin
@@ -20,6 +23,9 @@ function ServerlessClient(config) {
2023
this.setConfig(config)
2124
}
2225

26+
ServerlessClient.prototype = Object.create(EventEmitter.prototype);
27+
ServerlessClient.prototype.constructor = ServerlessClient;
28+
2329
ServerlessClient.prototype.constructor = ServerlessClient;
2430
ServerlessClient.prototype._sleep = delay =>
2531
new Promise(resolve => {
@@ -201,6 +207,7 @@ ServerlessClient.prototype._init = async function () {
201207
await this._setMaxConnections(this)
202208
}
203209

210+
this.emit("init", this._client);
204211
this._logger("Max connections: ", this._maxConns.cache.total)
205212
}
206213

@@ -423,8 +430,4 @@ ServerlessClient.prototype.end = async function () {
423430
this._client = null
424431
}
425432

426-
ServerlessClient.prototype.on = function (...args) {
427-
this._client.on(...args)
428-
}
429-
430433
module.exports = {ServerlessClient};

0 commit comments

Comments
 (0)