Skip to content

Commit

Permalink
feat: remove Socket#rooms object
Browse files Browse the repository at this point in the history
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 socketio#2890
  • Loading branch information
darrachequesne committed Sep 25, 2020
1 parent 6bdadc3 commit f6abefc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
27 changes: 9 additions & 18 deletions lib/socket.ts
Expand Up @@ -71,7 +71,6 @@ export class Socket extends EventEmitter {
public readonly id: SocketId;
public readonly handshake: Handshake;

public rooms = {};
public connected: boolean;
public disconnected: boolean;

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

if (!Array.isArray(rooms)) {
rooms = [rooms];
}
rooms = rooms.filter(room => {
return !this.rooms.hasOwnProperty(room);
});
if (!rooms.length) {
fn && fn(null);
return this;
}
this.adapter.addAll(this.id, rooms);
this.adapter.addAll(
this.id,
new Set(Array.isArray(rooms) ? rooms : [rooms])
);
debug("joined room %s", rooms);
rooms.forEach(room => {
this.rooms[room] = room;
});
fn && fn(null);
return this;
}
Expand All @@ -276,7 +265,6 @@ export class Socket extends EventEmitter {
this.adapter.del(this.id, room);

debug("left room %s", room);
delete this.rooms[room];
fn && fn(null);

return this;
Expand All @@ -287,7 +275,6 @@ export class Socket extends EventEmitter {
*/
private leaveAll(): void {
this.adapter.delAll(this.id);
this.rooms = {};
}

/**
Expand Down Expand Up @@ -584,4 +571,8 @@ export class Socket extends EventEmitter {
public get conn() {
return this.client.conn;
}

public get rooms(): Set<Room> {
return this.adapter.socketRooms(this.id) || new Set();
}
}
24 changes: 12 additions & 12 deletions test/socket.io.js
Expand Up @@ -682,12 +682,12 @@ describe("socket.io", () => {

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

s.on("disconnect", reason => {
expect(Object.keys(s.rooms)).to.eql([]);
expect(s.rooms.size).to.eql(0);
--total || done();
});
});
Expand Down Expand Up @@ -2150,15 +2150,15 @@ describe("socket.io", () => {
const socket = client(srv);
sio.on("connection", s => {
s.join("a", () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a"]);
expect(s.rooms).to.contain(s.id, "a");
s.join("b", () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b"]);
expect(s.rooms).to.contain(s.id, "a", "b");
s.join("c", () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b", "c"]);
expect(s.rooms).to.contain(s.id, "a", "b", "c");
s.leave("b", () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "c"]);
expect(s.rooms).to.contain(s.id, "a", "c");
s.leaveAll();
expect(Object.keys(s.rooms)).to.eql([]);
expect(s.rooms.size).to.eql(0);
done();
});
});
Expand Down Expand Up @@ -2194,13 +2194,13 @@ describe("socket.io", () => {
const socket = client(srv);
sio.on("connection", s => {
s.join("a", () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a"]);
expect(s.rooms).to.contain(s.id, "a");
s.join("b", () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b"]);
expect(s.rooms).to.contain(s.id, "a", "b");
s.leave("unknown", () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b"]);
expect(s.rooms).to.contain(s.id, "a", "b");
s.leaveAll();
expect(Object.keys(s.rooms)).to.eql([]);
expect(s.rooms.size).to.eql(0);
done();
});
});
Expand All @@ -2217,7 +2217,7 @@ describe("socket.io", () => {
const socket = client(srv);
sio.on("connection", s => {
s.join(["a", "b", "c"], () => {
expect(Object.keys(s.rooms)).to.eql([s.id, "a", "b", "c"]);
expect(s.rooms).to.contain(s.id, "a", "b", "c");
done();
});
});
Expand Down

0 comments on commit f6abefc

Please sign in to comment.