diff --git a/lib/index.ts b/lib/index.ts index bf51f80..f9266b9 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -82,14 +82,14 @@ export class Adapter extends EventEmitter { this._del(room, id); } - private _del(room, id) { - if (this.rooms.has(room)) { - const deleted = this.rooms.get(room).delete(id); + private _del(room: Room, id: SocketId) { + const _room = this.rooms.get(room); + if (_room != null) { + const deleted = _room.delete(id); if (deleted) { this.emit("leave-room", room, id); } - if (this.rooms.get(room).size === 0) { - this.rooms.delete(room); + if (_room.size === 0 && this.rooms.delete(room)) { this.emit("delete-room", room); } } diff --git a/test/index.js b/test/index.js index ce8b25e..f4729c4 100644 --- a/test/index.js +++ b/test/index.js @@ -230,6 +230,16 @@ describe("socket.io-adapter", () => { adapter.del("s1", "r1"); }); + it("should not throw when calling del twice", done => { + const adapter = new Adapter({ server: { encoder: null } }); + adapter.on("leave-room", (room, sid) => { + adapter.del("s1", "r1"); + process.nextTick(done); + }); + adapter.addAll("s1", new Set(["r1"])); + adapter.del("s1", "r1"); + }); + it("should emit a 'leave-room' event with delAll method", done => { const adapter = new Adapter({ server: { encoder: null } }); adapter.on("leave-room", (room, sid) => {