From 8a5db7fa36a075da75cde43cd4fb6382b7659953 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 15 Oct 2020 12:22:50 +0200 Subject: [PATCH] refactor: remove duplicate _sockets map Both the "connected" and the "_sockets" maps were used to track the Socket instances in the namespace. Let's merge them into "sockets". It's a breaking change, but: - the "sockets" object did already exist in Socket.IO v2 (and appears in some examples/tutorials) - "sockets" makes more sense than "connected" in my opinion - there was already a breaking change regarding the "connected" property (from object to Map) Breaking change: the "connected" map is renamed to "sockets" --- lib/index.ts | 2 +- lib/namespace.ts | 11 ++++------- lib/socket.ts | 2 -- package.json | 2 +- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 9a42ee5341..8f317a2870 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -568,7 +568,7 @@ export class Server extends EventEmitter { * @public */ public close(fn?: (err?: Error) => void): void { - for (const socket of this.sockets._sockets.values()) { + for (const socket of this.sockets.sockets.values()) { socket._onclose("server shutting down"); } diff --git a/lib/namespace.ts b/lib/namespace.ts index 61b3f510e3..feeaa15c33 100644 --- a/lib/namespace.ts +++ b/lib/namespace.ts @@ -10,7 +10,7 @@ const debug = debugModule("socket.io:namespace"); export class Namespace extends EventEmitter { public readonly name: string; - public readonly connected: Map = new Map(); + public readonly sockets: Map = new Map(); public adapter: Adapter; @@ -29,9 +29,6 @@ export class Namespace extends EventEmitter { /** @private */ _ids: number = 0; - /** @private */ - _sockets: Map = new Map(); - /** * Namespace constructor. * @@ -135,7 +132,7 @@ export class Namespace extends EventEmitter { if (err) return socket._error(err.message); // track socket - this._sockets.set(socket.id, socket); + this.sockets.set(socket.id, socket); // it's paramount that the internal `onconnect` logic // fires before user-set events to prevent state order @@ -161,8 +158,8 @@ export class Namespace extends EventEmitter { * @private */ _remove(socket: Socket): void { - if (this._sockets.has(socket.id)) { - this._sockets.delete(socket.id); + if (this.sockets.has(socket.id)) { + this.sockets.delete(socket.id); } else { debug("ignoring remove for %s", socket.id); } diff --git a/lib/socket.ts b/lib/socket.ts index b9d98557d7..d0d394f78b 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -291,7 +291,6 @@ export class Socket extends EventEmitter { */ _onconnect(): void { debug("socket connected - writing packet"); - this.nsp.connected.set(this.id, this); this.join(this.id); this.packet({ type: PacketType.CONNECT, data: { sid: this.id } }); } @@ -430,7 +429,6 @@ export class Socket extends EventEmitter { this.client._remove(this); this.connected = false; this.disconnected = true; - this.nsp.connected.delete(this.id); super.emit("disconnect", reason); } diff --git a/package.json b/package.json index 9c377f5ad0..b3ec23bfb0 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "base64id": "~2.0.0", "debug": "~4.1.0", "engine.io": "~4.0.0", - "socket.io-adapter": "~2.0.1", + "socket.io-adapter": "2.0.3-rc1", "socket.io-client": "3.0.0-rc1", "socket.io-parser": "4.0.1-rc2" },