Skip to content

Commit 706bb95

Browse files
committed
Fixed a memory leak issue when closing the ws
1 parent e6d2992 commit 706bb95

File tree

4 files changed

+18
-28
lines changed

4 files changed

+18
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "detritus-client-socket",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"description": "A TypeScript NodeJS library to interact with Discord's Gateway",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/basesocket.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ export class BaseSocket extends EventSpewer {
4343
constructor(url: string) {
4444
super();
4545
this.socket = new WebsocketDependency.module(url);
46+
this.socket.on(SocketEventsBase.CLOSE, this.onClose.bind(this));
4647
this.socket.on(SocketEventsBase.PONG, this.onPong.bind(this));
4748

48-
for (let event of Object.values(SocketEventsBase)) {
49-
if (event === SocketEventsBase.PONG) {
50-
continue;
51-
}
52-
this.socket.on(event, this.emit.bind(this, event));
53-
}
49+
this.socket.on(SocketEventsBase.ERROR, this.emit.bind(this, SocketEventsBase.ERROR));
50+
this.socket.on(SocketEventsBase.MESSAGE, this.emit.bind(this, SocketEventsBase.MESSAGE));
51+
this.socket.on(SocketEventsBase.OPEN, this.emit.bind(this, SocketEventsBase.OPEN));
52+
this.socket.on(SocketEventsBase.PING, this.emit.bind(this, SocketEventsBase.PING));
5453
}
5554

5655
get closed(): boolean {
@@ -73,41 +72,34 @@ export class BaseSocket extends EventSpewer {
7372
return WebsocketDependency.type;
7473
}
7574

76-
send(
77-
data: any,
78-
callback?: Function,
79-
): void {
75+
send(data: any, callback?: Function): void {
8076
if (this.connected) {
8177
this.socket.send(data, {}, callback);
8278
}
8379
}
8480

85-
close(
86-
code: number = SocketCloseCodes.NORMAL,
87-
reason: string = '',
88-
): void {
81+
close(code: number = SocketCloseCodes.NORMAL, reason: string = ''): void {
8982
if (this.connected) {
9083
this.socket.close(code, reason);
9184
}
85+
}
86+
87+
onClose(code: number, message: string): void {
9288
for (const [nonce, {reject}] of this.pings) {
9389
reject(new Error('Socket has closed.'));
9490
this.pings.delete(nonce);
9591
}
9692
this.pings.clear();
9793

98-
for (let event of Object.values(SocketEventsBase)) {
99-
// clear out all listeners but close from the socket
100-
if (event === SocketEventsBase.CLOSE) {
101-
continue;
102-
}
103-
this.socket.on(event, this.emit.bind(this, event));
104-
}
94+
this.socket.removeAllListeners();
95+
this.emit(SocketEventsBase.CLOSE, code, message);
96+
10597
this.removeAllListeners();
10698
}
10799

108100
onPong(data: any): void {
109101
try {
110-
const { nonce } = JSON.parse(String(data));
102+
const { nonce }: { nonce: string } = JSON.parse(String(data));
111103
const ping = this.pings.get(nonce);
112104
if (ping) {
113105
ping.resolve();
@@ -137,10 +129,7 @@ export class BaseSocket extends EventSpewer {
137129

138130
const now = Date.now();
139131
new Promise((res, rej) => {
140-
this.pings.set(nonce, {
141-
resolve: res,
142-
reject: rej,
143-
});
132+
this.pings.set(nonce, {resolve: res, reject: rej});
144133
this.socket.ping(JSON.stringify({nonce}));
145134
}).then(() => {
146135
expire.stop();

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const Package = Object.freeze({
22
URL: 'https://github.com/detritusjs/client-socket',
3-
VERSION: '0.4.2',
3+
VERSION: '0.4.3',
44
});
55

66
function normalize(object: {[key: string]: any}) {

src/gateway.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Package,
2020
SocketCloseCodes,
2121
SocketEvents,
22+
SocketEventsBase,
2223
SocketGatewayCloseCodes,
2324
SocketInternalCloseCodes,
2425
SocketInternalCloseReasons,

0 commit comments

Comments
 (0)