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"
This commit is contained in:
Damien Arrachequesne
2020-10-15 12:22:50 +02:00
parent 2a05042e2c
commit 8a5db7fa36
4 changed files with 6 additions and 11 deletions

View File

@@ -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");
}

View File

@@ -10,7 +10,7 @@ const debug = debugModule("socket.io:namespace");
export class Namespace extends EventEmitter {
public readonly name: string;
public readonly connected: Map<SocketId, Socket> = new Map();
public readonly sockets: Map<SocketId, Socket> = new Map();
public adapter: Adapter;
@@ -29,9 +29,6 @@ export class Namespace extends EventEmitter {
/** @private */
_ids: number = 0;
/** @private */
_sockets: Map<SocketId, Socket> = 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);
}

View File

@@ -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);
}

View File

@@ -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"
},