From c869488951af509230d4b975a7bf6ca45e2211c2 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 19 Mar 2023 10:48:11 -0400 Subject: [PATCH] *Track socks to reuse objects as we are storing state on them --- src/index.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index b39876d..aa858dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ export class SwarmClient extends Client { private _ready?: Promise; private _topics: Set = new Set(); + private _sockets: Map = new Map(); get dht() { const self = this; @@ -90,7 +91,14 @@ export class SwarmClient extends Client { "listenConnections", { swarm: this.swarm }, async (socketId: any) => { - this.emit("connection", await createSocket(socketId)); + const socket = + this._sockets.get(socketId) ?? (await createSocket(socketId)); + + socket.on("close", () => { + this._sockets.delete(socketId); + }); + + this.emit("connection", socket); } );