*Track socks to reuse objects as we are storing state on them

This commit is contained in:
Derrick Hammer 2023-03-19 10:48:11 -04:00
parent ab78add294
commit c869488951
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 9 additions and 1 deletions

View File

@ -17,6 +17,7 @@ export class SwarmClient extends Client {
private _ready?: Promise<void>;
private _topics: Set<Uint8Array> = new Set<Uint8Array>();
private _sockets: Map<number, Socket> = new Map<number, Socket>();
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);
}
);