Skip to content

Commit 1507b41

Browse files
committedSep 25, 2020
feat: remove Socket#rooms object
The value stored in the adapter will now be used, instead of duplicating it in the Socket class. Breaking change: Socket#rooms is now a Set instead of an object Closes #2890
1 parent 84437dc commit 1507b41

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed
 

‎lib/socket.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export class Socket extends EventEmitter {
7171
public readonly id: SocketId;
7272
public readonly handshake: Handshake;
7373

74-
public rooms = {};
7574
public connected: boolean;
7675
public disconnected: boolean;
7776

@@ -242,24 +241,14 @@ export class Socket extends EventEmitter {
242241
* @param {Function} fn - optional, callback
243242
* @return {Socket} self
244243
*/
245-
public join(rooms, fn?: (err: Error) => void): Socket {
244+
public join(rooms: Room | Array<Room>, fn?: (err: Error) => void): Socket {
246245
debug("joining room %s", rooms);
247246

248-
if (!Array.isArray(rooms)) {
249-
rooms = [rooms];
250-
}
251-
rooms = rooms.filter(room => {
252-
return !this.rooms.hasOwnProperty(room);
253-
});
254-
if (!rooms.length) {
255-
fn && fn(null);
256-
return this;
257-
}
258-
this.adapter.addAll(this.id, rooms);
247+
this.adapter.addAll(
248+
this.id,
249+
new Set(Array.isArray(rooms) ? rooms : [rooms])
250+
);
259251
debug("joined room %s", rooms);
260-
rooms.forEach(room => {
261-
this.rooms[room] = room;
262-
});
263252
fn && fn(null);
264253
return this;
265254
}
@@ -276,7 +265,6 @@ export class Socket extends EventEmitter {
276265
this.adapter.del(this.id, room);
277266

278267
debug("left room %s", room);
279-
delete this.rooms[room];
280268
fn && fn(null);
281269

282270
return this;
@@ -287,7 +275,6 @@ export class Socket extends EventEmitter {
287275
*/
288276
private leaveAll(): void {
289277
this.adapter.delAll(this.id);
290-
this.rooms = {};
291278
}
292279

293280
/**
@@ -584,4 +571,8 @@ export class Socket extends EventEmitter {
584571
public get conn() {
585572
return this.client.conn;
586573
}
574+
575+
public get rooms(): Set<Room> {
576+
return this.adapter.socketRooms(this.id) || new Set();
577+
}
587578
}

‎test/socket.io.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,12 @@ describe("socket.io", () => {
682682

683683
let total = 2;
684684
s.on("disconnecting", reason => {
685-
expect(Object.keys(s.rooms)).to.eql([s.id, "a"]);
685+
expect(s.rooms).to.contain(s.id, "a");
686686
total--;
687687
});
688688

689689
s.on("disconnect", reason => {
690-
expect(Object.keys(s.rooms)).to.eql([]);
690+
expect(s.rooms.size).to.eql(0);
691691
--total || done();
692692
});
693693
});
@@ -2150,15 +2150,15 @@ describe("socket.io", () => {
21502150
const socket = client(srv);
21512151
sio.on("connection", s => {
21522152
s.join("a", () => {
2153-
expect(Object.keys(s.rooms)).to.eql([s.id, "a"]);
2153+
expect(s.rooms).to.contain(s.id, "a");
21542154
s.join("b", () => {
2155-
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b"]);
2155+
expect(s.rooms).to.contain(s.id, "a", "b");
21562156
s.join("c", () => {
2157-
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b", "c"]);
2157+
expect(s.rooms).to.contain(s.id, "a", "b", "c");
21582158
s.leave("b", () => {
2159-
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "c"]);
2159+
expect(s.rooms).to.contain(s.id, "a", "c");
21602160
s.leaveAll();
2161-
expect(Object.keys(s.rooms)).to.eql([]);
2161+
expect(s.rooms.size).to.eql(0);
21622162
done();
21632163
});
21642164
});
@@ -2194,13 +2194,13 @@ describe("socket.io", () => {
21942194
const socket = client(srv);
21952195
sio.on("connection", s => {
21962196
s.join("a", () => {
2197-
expect(Object.keys(s.rooms)).to.eql([s.id, "a"]);
2197+
expect(s.rooms).to.contain(s.id, "a");
21982198
s.join("b", () => {
2199-
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b"]);
2199+
expect(s.rooms).to.contain(s.id, "a", "b");
22002200
s.leave("unknown", () => {
2201-
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b"]);
2201+
expect(s.rooms).to.contain(s.id, "a", "b");
22022202
s.leaveAll();
2203-
expect(Object.keys(s.rooms)).to.eql([]);
2203+
expect(s.rooms.size).to.eql(0);
22042204
done();
22052205
});
22062206
});
@@ -2217,7 +2217,7 @@ describe("socket.io", () => {
22172217
const socket = client(srv);
22182218
sio.on("connection", s => {
22192219
s.join(["a", "b", "c"], () => {
2220-
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b", "c"]);
2220+
expect(s.rooms).to.contain(s.id, "a", "b", "c");
22212221
done();
22222222
});
22232223
});

0 commit comments

Comments
 (0)
Please sign in to comment.