Skip to content

Commit 8a5db7f

Browse files
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"
1 parent 2a05042 commit 8a5db7f

File tree

4 files changed

+6
-11
lines changed

4 files changed

+6
-11
lines changed

Diff for: lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ export class Server extends EventEmitter {
568568
* @public
569569
*/
570570
public close(fn?: (err?: Error) => void): void {
571-
for (const socket of this.sockets._sockets.values()) {
571+
for (const socket of this.sockets.sockets.values()) {
572572
socket._onclose("server shutting down");
573573
}
574574

Diff for: lib/namespace.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const debug = debugModule("socket.io:namespace");
1010

1111
export class Namespace extends EventEmitter {
1212
public readonly name: string;
13-
public readonly connected: Map<SocketId, Socket> = new Map();
13+
public readonly sockets: Map<SocketId, Socket> = new Map();
1414

1515
public adapter: Adapter;
1616

@@ -29,9 +29,6 @@ export class Namespace extends EventEmitter {
2929
/** @private */
3030
_ids: number = 0;
3131

32-
/** @private */
33-
_sockets: Map<SocketId, Socket> = new Map();
34-
3532
/**
3633
* Namespace constructor.
3734
*
@@ -135,7 +132,7 @@ export class Namespace extends EventEmitter {
135132
if (err) return socket._error(err.message);
136133

137134
// track socket
138-
this._sockets.set(socket.id, socket);
135+
this.sockets.set(socket.id, socket);
139136

140137
// it's paramount that the internal `onconnect` logic
141138
// fires before user-set events to prevent state order
@@ -161,8 +158,8 @@ export class Namespace extends EventEmitter {
161158
* @private
162159
*/
163160
_remove(socket: Socket): void {
164-
if (this._sockets.has(socket.id)) {
165-
this._sockets.delete(socket.id);
161+
if (this.sockets.has(socket.id)) {
162+
this.sockets.delete(socket.id);
166163
} else {
167164
debug("ignoring remove for %s", socket.id);
168165
}

Diff for: lib/socket.ts

-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ export class Socket extends EventEmitter {
291291
*/
292292
_onconnect(): void {
293293
debug("socket connected - writing packet");
294-
this.nsp.connected.set(this.id, this);
295294
this.join(this.id);
296295
this.packet({ type: PacketType.CONNECT, data: { sid: this.id } });
297296
}
@@ -430,7 +429,6 @@ export class Socket extends EventEmitter {
430429
this.client._remove(this);
431430
this.connected = false;
432431
this.disconnected = true;
433-
this.nsp.connected.delete(this.id);
434432
super.emit("disconnect", reason);
435433
}
436434

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"base64id": "~2.0.0",
3838
"debug": "~4.1.0",
3939
"engine.io": "~4.0.0",
40-
"socket.io-adapter": "~2.0.1",
40+
"socket.io-adapter": "2.0.3-rc1",
4141
"socket.io-client": "3.0.0-rc1",
4242
"socket.io-parser": "4.0.1-rc2"
4343
},

0 commit comments

Comments
 (0)