Skip to content

Commit

Permalink
feat: allow to pass an array to io.to(...)
Browse files Browse the repository at this point in the history
In some cases it is necessary to pass an array of rooms instead of a single room.

New syntax:

```
io.to(["room1", "room2"]).except(["room3"]).emit(...);

socket.to(["room1", "room2"]).except(["room3"]).emit(...);
```

Related: #3048
  • Loading branch information
darrachequesne committed Mar 1, 2021
1 parent ac9e8ca commit 085d1de
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
22 changes: 17 additions & 5 deletions lib/broadcast-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ export class BroadcastOperator {
* @return a new BroadcastOperator instance
* @public
*/
public to(room: Room): BroadcastOperator {
public to(room: Room | Room[]): BroadcastOperator {
const rooms = new Set(this.rooms);
if (Array.isArray(room)) {
room.forEach((r) => rooms.add(r));
} else {
rooms.add(room);
}
return new BroadcastOperator(
this.adapter,
new Set([...this.rooms, room]),
rooms,
this.exceptRooms,
this.flags
);
Expand All @@ -34,7 +40,7 @@ export class BroadcastOperator {
* @return a new BroadcastOperator instance
* @public
*/
public in(room: Room): BroadcastOperator {
public in(room: Room | Room[]): BroadcastOperator {
return this.to(room);
}

Expand All @@ -45,11 +51,17 @@ export class BroadcastOperator {
* @return a new BroadcastOperator instance
* @public
*/
public except(room: Room): BroadcastOperator {
public except(room: Room | Room[]): BroadcastOperator {
const exceptRooms = new Set(this.exceptRooms);
if (Array.isArray(room)) {
room.forEach((r) => exceptRooms.add(r));
} else {
exceptRooms.add(room);
}
return new BroadcastOperator(
this.adapter,
this.rooms,
new Set([...this.exceptRooms, room]),
exceptRooms,
this.flags
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public to(room: Room): BroadcastOperator {
public to(room: Room | Room[]): BroadcastOperator {
return this.sockets.to(room);
}

Expand All @@ -640,7 +640,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public in(room: Room): BroadcastOperator {
public in(room: Room | Room[]): BroadcastOperator {
return this.sockets.in(room);
}

Expand All @@ -651,7 +651,7 @@ export class Server extends EventEmitter {
* @return self
* @public
*/
public except(name: Room): Server {
public except(name: Room | Room[]): Server {
this.sockets.except(name);
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class Namespace extends EventEmitter {
* @return self
* @public
*/
public to(room: Room): BroadcastOperator {
public to(room: Room | Room[]): BroadcastOperator {
return new BroadcastOperator(this.adapter).to(room);
}

Expand All @@ -111,7 +111,7 @@ export class Namespace extends EventEmitter {
* @return self
* @public
*/
public in(room: Room): BroadcastOperator {
public in(room: Room | Room[]): BroadcastOperator {
return new BroadcastOperator(this.adapter).in(room);
}

Expand All @@ -122,7 +122,7 @@ export class Namespace extends EventEmitter {
* @return self
* @public
*/
public except(room: Room): BroadcastOperator {
public except(room: Room | Room[]): BroadcastOperator {
return new BroadcastOperator(this.adapter).except(room);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Socket extends EventEmitter {
* @return self
* @public
*/
public to(room: Room): BroadcastOperator {
public to(room: Room | Room[]): BroadcastOperator {
return this.newBroadcastOperator().to(room);
}

Expand All @@ -186,7 +186,7 @@ export class Socket extends EventEmitter {
* @return self
* @public
*/
public in(room: Room): BroadcastOperator {
public in(room: Room | Room[]): BroadcastOperator {
return this.newBroadcastOperator().in(room);
}

Expand All @@ -197,7 +197,7 @@ export class Socket extends EventEmitter {
* @return self
* @public
*/
public except(room: Room): BroadcastOperator {
public except(room: Room | Room[]): BroadcastOperator {
return this.newBroadcastOperator().except(room);
}

Expand Down
5 changes: 2 additions & 3 deletions test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ describe("socket.io", () => {

it("should return an immutable broadcast operator", () => {
const sio = new Server();
const operator = sio.local.to("room1").to("room2").except("room3");
const operator = sio.local.to(["room1", "room2"]).except("room3");
operator.compress(true).emit("hello");
operator.volatile.emit("hello");
operator.to("room4").emit("hello");
Expand Down Expand Up @@ -2362,8 +2362,7 @@ describe("socket.io", () => {
sio.on("connection", (socket: Socket) => {
const operator = socket.local
.compress(false)
.to("room1")
.to("room2")
.to(["room1", "room2"])
.except("room3");
operator.compress(true).emit("hello");
operator.volatile.emit("hello");
Expand Down

0 comments on commit 085d1de

Please sign in to comment.