Skip to content

Commit 424a473

Browse files
refactor: use ES6 Maps instead of plain objects
These attributes were not part of the public API, so there's no breaking change.
1 parent 1507b41 commit 424a473

File tree

5 files changed

+53
-57
lines changed

5 files changed

+53
-57
lines changed

lib/client.ts

+24-30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import debugModule = require("debug");
44
import { IncomingMessage } from "http";
55
import { Server } from "./index";
66
import { Socket } from "./socket";
7+
import { SocketId } from "socket.io-adapter";
78

89
const debug = debugModule("socket.io:client");
910

@@ -15,8 +16,8 @@ export class Client {
1516
private readonly server;
1617
private readonly encoder;
1718
private readonly decoder;
18-
private sockets: object = {};
19-
private nsps: object = {};
19+
private sockets: Map<SocketId, Socket> = new Map();
20+
private nsps: Map<string, Socket> = new Map();
2021
private connectBuffer: Array<string> = [];
2122

2223
/**
@@ -65,7 +66,7 @@ export class Client {
6566
* @package
6667
*/
6768
public connect(name, query = {}) {
68-
if (this.server.nsps[name]) {
69+
if (this.server.nsps.has(name)) {
6970
debug("connecting to namespace %s", name);
7071
return this.doConnect(name, query);
7172
}
@@ -94,19 +95,18 @@ export class Client {
9495
private doConnect(name, query) {
9596
const nsp = this.server.of(name);
9697

97-
if ("/" != name && !this.nsps["/"]) {
98+
if ("/" != name && !this.nsps.has("/")) {
9899
this.connectBuffer.push(name);
99100
return;
100101
}
101102

102-
const self = this;
103-
const socket = nsp.add(this, query, function() {
104-
self.sockets[socket.id] = socket;
105-
self.nsps[nsp.name] = socket;
103+
const socket = nsp.add(this, query, () => {
104+
this.sockets.set(socket.id, socket);
105+
this.nsps.set(nsp.name, socket);
106106

107-
if ("/" == nsp.name && self.connectBuffer.length > 0) {
108-
self.connectBuffer.forEach(self.connect, self);
109-
self.connectBuffer = [];
107+
if ("/" == nsp.name && this.connectBuffer.length > 0) {
108+
this.connectBuffer.forEach(this.connect, this);
109+
this.connectBuffer = [];
110110
}
111111
});
112112
}
@@ -117,12 +117,10 @@ export class Client {
117117
* @package
118118
*/
119119
public disconnect() {
120-
for (const id in this.sockets) {
121-
if (this.sockets.hasOwnProperty(id)) {
122-
this.sockets[id].disconnect();
123-
}
120+
for (const socket of this.sockets.values()) {
121+
socket.disconnect();
124122
}
125-
this.sockets = {};
123+
this.sockets.clear();
126124
this.close();
127125
}
128126

@@ -132,10 +130,10 @@ export class Client {
132130
* @package
133131
*/
134132
public remove(socket: Socket) {
135-
if (this.sockets.hasOwnProperty(socket.id)) {
136-
const nsp = this.sockets[socket.id].nsp.name;
137-
delete this.sockets[socket.id];
138-
delete this.nsps[nsp];
133+
if (this.sockets.has(socket.id)) {
134+
const nsp = this.sockets.get(socket.id).nsp.name;
135+
this.sockets.delete(socket.id);
136+
this.nsps.delete(nsp);
139137
} else {
140138
debug("ignoring remove for %s", socket.id);
141139
}
@@ -207,7 +205,7 @@ export class Client {
207205
url.parse(packet.nsp, true).query
208206
);
209207
} else {
210-
const socket = this.nsps[packet.nsp];
208+
const socket = this.nsps.get(packet.nsp);
211209
if (socket) {
212210
process.nextTick(function() {
213211
socket.onpacket(packet);
@@ -224,10 +222,8 @@ export class Client {
224222
* @param {Object} err object
225223
*/
226224
private onerror(err) {
227-
for (const id in this.sockets) {
228-
if (this.sockets.hasOwnProperty(id)) {
229-
this.sockets[id].onerror(err);
230-
}
225+
for (const socket of this.sockets.values()) {
226+
socket.onerror(err);
231227
}
232228
this.conn.close();
233229
}
@@ -244,12 +240,10 @@ export class Client {
244240
this.destroy();
245241

246242
// `nsps` and `sockets` are cleaned up seamlessly
247-
for (const id in this.sockets) {
248-
if (this.sockets.hasOwnProperty(id)) {
249-
this.sockets[id].onclose(reason);
250-
}
243+
for (const socket of this.sockets.values()) {
244+
socket.onclose(reason);
251245
}
252-
this.sockets = {};
246+
this.sockets.clear();
253247

254248
this.decoder.destroy(); // clean up decoder
255249
}

lib/index.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Server extends EventEmitter {
3131
/** @package */
3232
public readonly encoder;
3333

34-
private nsps: object = {};
34+
private nsps: Map<string, Namespace> = new Map();
3535
private parentNsps: Map<
3636
| string
3737
| RegExp
@@ -224,10 +224,8 @@ class Server extends EventEmitter {
224224
public adapter(v) {
225225
if (!arguments.length) return this._adapter;
226226
this._adapter = v;
227-
for (const i in this.nsps) {
228-
if (this.nsps.hasOwnProperty(i)) {
229-
this.nsps[i].initAdapter();
230-
}
227+
for (const nsp of this.nsps.values()) {
228+
nsp.initAdapter();
231229
}
232230
return this;
233231
}
@@ -507,11 +505,11 @@ class Server extends EventEmitter {
507505

508506
if (String(name)[0] !== "/") name = "/" + name;
509507

510-
let nsp = this.nsps[name];
508+
let nsp = this.nsps.get(name);
511509
if (!nsp) {
512510
debug("initializing namespace %s", name);
513511
nsp = new Namespace(this, name);
514-
this.nsps[name] = nsp;
512+
this.nsps.set(name, nsp);
515513
}
516514
if (fn) nsp.on("connect", fn);
517515
return nsp;
@@ -523,10 +521,8 @@ class Server extends EventEmitter {
523521
* @param {Function} [fn] optional, called as `fn([err])` on error OR all conns closed
524522
*/
525523
public close(fn: (err?: Error) => void): void {
526-
for (const id in this.nsps["/"].sockets) {
527-
if (this.nsps["/"].sockets.hasOwnProperty(id)) {
528-
this.nsps["/"].sockets[id].onclose();
529-
}
524+
for (const socket of this.sockets.sockets.values()) {
525+
socket.onclose("server shutting down");
530526
}
531527

532528
this.engine.close();

lib/namespace.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export class Namespace extends EventEmitter {
3535
public flags: any = {};
3636
/** @package */
3737
public ids: number = 0;
38-
39-
private readonly sockets: object = {};
38+
/** @package */
39+
public sockets: Map<SocketId, Socket> = new Map();
4040

4141
/**
4242
* Namespace constructor.
@@ -55,8 +55,10 @@ export class Namespace extends EventEmitter {
5555
* Initializes the `Adapter` for this nsp.
5656
* Run upon changing adapter by `Server#adapter`
5757
* in addition to the constructor.
58+
*
59+
* @package
5860
*/
59-
private initAdapter(): void {
61+
public initAdapter(): void {
6062
this.adapter = new (this.server.adapter())(this);
6163
}
6264

@@ -132,14 +134,13 @@ export class Namespace extends EventEmitter {
132134
private add(client: Client, query, fn?: () => void): Socket {
133135
debug("adding socket to nsp %s", this.name);
134136
const socket = new Socket(this, client, query);
135-
const self = this;
136137
this.run(socket, err => {
137138
process.nextTick(() => {
138139
if ("open" == client.conn.readyState) {
139140
if (err) return socket.error(err.message);
140141

141142
// track socket
142-
self.sockets[socket.id] = socket;
143+
this.sockets.set(socket.id, socket);
143144

144145
// it's paramount that the internal `onconnect` logic
145146
// fires before user-set events to prevent state order
@@ -165,8 +166,8 @@ export class Namespace extends EventEmitter {
165166
* @package
166167
*/
167168
public remove(socket: Socket): void {
168-
if (this.sockets.hasOwnProperty(socket.id)) {
169-
delete this.sockets[socket.id];
169+
if (this.sockets.has(socket.id)) {
170+
this.sockets.delete(socket.id);
170171
} else {
171172
debug("ignoring remove for %s", socket.id);
172173
}

lib/parent-namespace.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ export class ParentNamespace extends Namespace {
1010

1111
initAdapter() {}
1212

13-
// @ts-ignore
14-
public emit(...args) {
13+
public emit(...args): Namespace {
1514
this.children.forEach(nsp => {
1615
nsp.rooms = this.rooms;
1716
nsp.flags = this.flags;
1817
nsp.emit.apply(nsp, args);
1918
});
2019
this.rooms.clear();
2120
this.flags = {};
21+
22+
return this;
2223
}
2324

2425
createChild(name) {
@@ -33,7 +34,7 @@ export class ParentNamespace extends Namespace {
3334
namespace.on("connection", listener)
3435
);
3536
this.children.add(namespace);
36-
this.server.nsps[name] = namespace;
37+
this.server.nsps.set(name, namespace);
3738
return namespace;
3839
}
3940
}

lib/socket.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class Socket extends EventEmitter {
7676

7777
private readonly server: Server;
7878
private readonly adapter: Adapter;
79-
private acks: object = {};
79+
private acks: Map<number, () => void> = new Map();
8080
private fns: Array<
8181
(event: Array<any>, next: (err: Error) => void) => void
8282
> = [];
@@ -154,7 +154,7 @@ export class Socket extends EventEmitter {
154154
}
155155

156156
debug("emitting packet with ack id %d", this.nsp.ids);
157-
this.acks[this.nsp.ids] = args.pop();
157+
this.acks.set(this.nsp.ids, args.pop());
158158
packet.id = this.nsp.ids++;
159159
}
160160

@@ -376,11 +376,11 @@ export class Socket extends EventEmitter {
376376
* Called upon ack packet.
377377
*/
378378
private onack(packet): void {
379-
const ack = this.acks[packet.id];
379+
const ack = this.acks.get(packet.id);
380380
if ("function" == typeof ack) {
381381
debug("calling ack %s with %j", packet.id, packet.data);
382382
ack.apply(this, packet.data);
383-
delete this.acks[packet.id];
383+
this.acks.delete(packet.id);
384384
} else {
385385
debug("bad ack %s", packet.id);
386386
}
@@ -396,8 +396,10 @@ export class Socket extends EventEmitter {
396396

397397
/**
398398
* Handles a client error.
399+
*
400+
* @package
399401
*/
400-
private onerror(err): void {
402+
public onerror(err): void {
401403
if (this.listeners("error").length) {
402404
super.emit("error", err);
403405
} else {
@@ -411,8 +413,10 @@ export class Socket extends EventEmitter {
411413
*
412414
* @param {String} reason
413415
* @throw {Error} optional error object
416+
*
417+
* @package
414418
*/
415-
private onclose(reason: string) {
419+
public onclose(reason: string) {
416420
if (!this.connected) return this;
417421
debug("closing socket - reason %s", reason);
418422
super.emit("disconnecting", reason);

0 commit comments

Comments
 (0)