diff --git a/lib/broadcast-operator.ts b/lib/broadcast-operator.ts index 889eef9d3f..9264622478 100644 --- a/lib/broadcast-operator.ts +++ b/lib/broadcast-operator.ts @@ -26,14 +26,14 @@ export class BroadcastOperator * @return a new BroadcastOperator instance * @public */ - public to(room: Room | Room[]): BroadcastOperator { + public to(room: Room | Room[]) { const rooms = new Set(this.rooms); if (Array.isArray(room)) { room.forEach((r) => rooms.add(r)); } else { rooms.add(room); } - return new BroadcastOperator( + return new BroadcastOperator( this.adapter, rooms, this.exceptRooms, @@ -48,7 +48,7 @@ export class BroadcastOperator * @return a new BroadcastOperator instance * @public */ - public in(room: Room | Room[]): BroadcastOperator { + public in(room: Room | Room[]) { return this.to(room); } @@ -59,16 +59,14 @@ export class BroadcastOperator * @return a new BroadcastOperator instance * @public */ - public except( - room: Room | Room[] - ): BroadcastOperator { + public except(room: Room | Room[]) { const exceptRooms = new Set(this.exceptRooms); if (Array.isArray(room)) { room.forEach((r) => exceptRooms.add(r)); } else { exceptRooms.add(room); } - return new BroadcastOperator( + return new BroadcastOperator( this.adapter, this.rooms, exceptRooms, @@ -83,11 +81,9 @@ export class BroadcastOperator * @return a new BroadcastOperator instance * @public */ - public compress( - compress: boolean - ): BroadcastOperator { + public compress(compress: boolean) { const flags = Object.assign({}, this.flags, { compress }); - return new BroadcastOperator( + return new BroadcastOperator( this.adapter, this.rooms, this.exceptRooms, @@ -103,9 +99,9 @@ export class BroadcastOperator * @return a new BroadcastOperator instance * @public */ - public get volatile(): BroadcastOperator { + public get volatile() { const flags = Object.assign({}, this.flags, { volatile: true }); - return new BroadcastOperator( + return new BroadcastOperator( this.adapter, this.rooms, this.exceptRooms, @@ -119,9 +115,9 @@ export class BroadcastOperator * @return a new BroadcastOperator instance * @public */ - public get local(): BroadcastOperator { + public get local() { const flags = Object.assign({}, this.flags, { local: true }); - return new BroadcastOperator( + return new BroadcastOperator( this.adapter, this.rooms, this.exceptRooms, @@ -144,7 +140,7 @@ export class BroadcastOperator */ public timeout(timeout: number) { const flags = Object.assign({}, this.flags, { timeout }); - return new BroadcastOperator( + return new BroadcastOperator( this.adapter, this.rooms, this.exceptRooms, @@ -255,9 +251,7 @@ export class BroadcastOperator * * @public */ - public fetchSockets(): Promise< - RemoteSocket[] - > { + public fetchSockets(): Promise[]> { return this.adapter .fetchSockets({ rooms: this.rooms, @@ -359,7 +353,10 @@ export class RemoteSocket this.handshake = details.handshake; this.rooms = new Set(details.rooms); this.data = details.data; - this.operator = new BroadcastOperator(adapter, new Set([this.id])); + this.operator = new BroadcastOperator( + adapter, + new Set([this.id]) + ); } public emit>( diff --git a/lib/index.ts b/lib/index.ts index cfaa49781d..29bf5e2ddb 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -819,7 +819,7 @@ export class Server< * @param room * @public */ - public socketsJoin(room: Room | Room[]): void { + public socketsJoin(room: Room | Room[]) { return this.sockets.socketsJoin(room); } @@ -829,7 +829,7 @@ export class Server< * @param room * @public */ - public socketsLeave(room: Room | Room[]): void { + public socketsLeave(room: Room | Room[]) { return this.sockets.socketsLeave(room); } @@ -839,7 +839,7 @@ export class Server< * @param close - whether to close the underlying connection * @public */ - public disconnectSockets(close: boolean = false): void { + public disconnectSockets(close: boolean = false) { return this.sockets.disconnectSockets(close); } } diff --git a/lib/namespace.ts b/lib/namespace.ts index 2f5eede183..b769bf0f79 100644 --- a/lib/namespace.ts +++ b/lib/namespace.ts @@ -175,8 +175,8 @@ export class Namespace< * @return self * @public */ - public to(room: Room | Room[]): BroadcastOperator { - return new BroadcastOperator(this.adapter).to(room); + public to(room: Room | Room[]) { + return new BroadcastOperator(this.adapter).to(room); } /** @@ -186,8 +186,8 @@ export class Namespace< * @return self * @public */ - public in(room: Room | Room[]): BroadcastOperator { - return new BroadcastOperator(this.adapter).in(room); + public in(room: Room | Room[]) { + return new BroadcastOperator(this.adapter).in(room); } /** @@ -197,10 +197,10 @@ export class Namespace< * @return self * @public */ - public except( - room: Room | Room[] - ): BroadcastOperator { - return new BroadcastOperator(this.adapter).except(room); + public except(room: Room | Room[]) { + return new BroadcastOperator(this.adapter).except( + room + ); } /** @@ -345,7 +345,9 @@ export class Namespace< * @public */ public allSockets(): Promise> { - return new BroadcastOperator(this.adapter).allSockets(); + return new BroadcastOperator( + this.adapter + ).allSockets(); } /** @@ -355,10 +357,10 @@ export class Namespace< * @return self * @public */ - public compress( - compress: boolean - ): BroadcastOperator { - return new BroadcastOperator(this.adapter).compress(compress); + public compress(compress: boolean) { + return new BroadcastOperator(this.adapter).compress( + compress + ); } /** @@ -369,8 +371,8 @@ export class Namespace< * @return self * @public */ - public get volatile(): BroadcastOperator { - return new BroadcastOperator(this.adapter).volatile; + public get volatile() { + return new BroadcastOperator(this.adapter).volatile; } /** @@ -379,8 +381,8 @@ export class Namespace< * @return self * @public */ - public get local(): BroadcastOperator { - return new BroadcastOperator(this.adapter).local; + public get local() { + return new BroadcastOperator(this.adapter).local; } /** @@ -397,7 +399,9 @@ export class Namespace< * @param timeout */ public timeout(timeout: number) { - return new BroadcastOperator(this.adapter).timeout(timeout); + return new BroadcastOperator(this.adapter).timeout( + timeout + ); } /** @@ -405,8 +409,10 @@ export class Namespace< * * @public */ - public fetchSockets(): Promise[]> { - return new BroadcastOperator(this.adapter).fetchSockets(); + public fetchSockets() { + return new BroadcastOperator( + this.adapter + ).fetchSockets(); } /** @@ -415,8 +421,10 @@ export class Namespace< * @param room * @public */ - public socketsJoin(room: Room | Room[]): void { - return new BroadcastOperator(this.adapter).socketsJoin(room); + public socketsJoin(room: Room | Room[]) { + return new BroadcastOperator( + this.adapter + ).socketsJoin(room); } /** @@ -425,8 +433,10 @@ export class Namespace< * @param room * @public */ - public socketsLeave(room: Room | Room[]): void { - return new BroadcastOperator(this.adapter).socketsLeave(room); + public socketsLeave(room: Room | Room[]) { + return new BroadcastOperator( + this.adapter + ).socketsLeave(room); } /** @@ -435,7 +445,9 @@ export class Namespace< * @param close - whether to close the underlying connection * @public */ - public disconnectSockets(close: boolean = false): void { - return new BroadcastOperator(this.adapter).disconnectSockets(close); + public disconnectSockets(close: boolean = false) { + return new BroadcastOperator( + this.adapter + ).disconnectSockets(close); } } diff --git a/lib/socket.ts b/lib/socket.ts index b737a97f51..c9079efa37 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -902,10 +902,10 @@ export class Socket< } } - private newBroadcastOperator(): BroadcastOperator { + private newBroadcastOperator() { const flags = Object.assign({}, this.flags); this.flags = {}; - return new BroadcastOperator( + return new BroadcastOperator( this.adapter, new Set(), new Set([this.id]), diff --git a/test/socket.io.test-d.ts b/test/socket.io.test-d.ts index 001951afe0..bb68400587 100644 --- a/test/socket.io.test-d.ts +++ b/test/socket.io.test-d.ts @@ -209,6 +209,10 @@ describe("server", () => { const srv = createServer(); const sio = new Server(srv); srv.listen(() => { + sio.emit("helloFromServer", "hi", 1); + sio.to("room").emit("helloFromServer", "hi", 1); + sio.timeout(1000).emit("helloFromServer", "hi", 1); + sio.on("connection", (s) => { s.emit("helloFromServer", "hi", 10); done(); @@ -220,6 +224,10 @@ describe("server", () => { const srv = createServer(); const sio = new Server(srv); srv.listen(() => { + expectError(sio.emit("helloFromClient")); + expectError(sio.to("room").emit("helloFromClient")); + expectError(sio.timeout(1000).to("room").emit("helloFromClient")); + sio.on("connection", (s) => { expectError(s.emit("helloFromClient", "hi")); expectError(s.emit("helloFromServer", "hi", 10, "10")); @@ -234,7 +242,7 @@ describe("server", () => { }); }); - describe("listen and emit event maps", () => { + describe("listen and emit event maps for the serverSideEmit method", () => { interface ClientToServerEvents { helloFromClient: (message: string) => void; }