Skip to content

Commit

Permalink
fix(typings): apply types to "io.timeout(...).emit()" calls
Browse files Browse the repository at this point in the history
Typed events were not applied when calling "io.timeout(...).emit()".

Related: socketio/socket.io-client#1555 (comment)

Reference: https://socket.io/docs/v4/typescript/
  • Loading branch information
darrachequesne committed Oct 13, 2022
1 parent 10fa4a2 commit e357daf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 52 deletions.
37 changes: 17 additions & 20 deletions lib/broadcast-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
* @return a new BroadcastOperator instance
* @public
*/
public to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
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<EmitEvents, SocketData>(
this.adapter,
rooms,
this.exceptRooms,
Expand All @@ -48,7 +48,7 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
* @return a new BroadcastOperator instance
* @public
*/
public in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
public in(room: Room | Room[]) {
return this.to(room);
}

Expand All @@ -59,16 +59,14 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
* @return a new BroadcastOperator instance
* @public
*/
public except(
room: Room | Room[]
): BroadcastOperator<EmitEvents, SocketData> {
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<EmitEvents, SocketData>(
this.adapter,
this.rooms,
exceptRooms,
Expand All @@ -83,11 +81,9 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
* @return a new BroadcastOperator instance
* @public
*/
public compress(
compress: boolean
): BroadcastOperator<EmitEvents, SocketData> {
public compress(compress: boolean) {
const flags = Object.assign({}, this.flags, { compress });
return new BroadcastOperator(
return new BroadcastOperator<EmitEvents, SocketData>(
this.adapter,
this.rooms,
this.exceptRooms,
Expand All @@ -103,9 +99,9 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
* @return a new BroadcastOperator instance
* @public
*/
public get volatile(): BroadcastOperator<EmitEvents, SocketData> {
public get volatile() {
const flags = Object.assign({}, this.flags, { volatile: true });
return new BroadcastOperator(
return new BroadcastOperator<EmitEvents, SocketData>(
this.adapter,
this.rooms,
this.exceptRooms,
Expand All @@ -119,9 +115,9 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
* @return a new BroadcastOperator instance
* @public
*/
public get local(): BroadcastOperator<EmitEvents, SocketData> {
public get local() {
const flags = Object.assign({}, this.flags, { local: true });
return new BroadcastOperator(
return new BroadcastOperator<EmitEvents, SocketData>(
this.adapter,
this.rooms,
this.exceptRooms,
Expand All @@ -144,7 +140,7 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
*/
public timeout(timeout: number) {
const flags = Object.assign({}, this.flags, { timeout });
return new BroadcastOperator(
return new BroadcastOperator<EmitEvents, SocketData>(
this.adapter,
this.rooms,
this.exceptRooms,
Expand Down Expand Up @@ -255,9 +251,7 @@ export class BroadcastOperator<EmitEvents extends EventsMap, SocketData>
*
* @public
*/
public fetchSockets<SocketData = any>(): Promise<
RemoteSocket<EmitEvents, SocketData>[]
> {
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
return this.adapter
.fetchSockets({
rooms: this.rooms,
Expand Down Expand Up @@ -359,7 +353,10 @@ export class RemoteSocket<EmitEvents extends EventsMap, SocketData>
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<EmitEvents, SocketData>(
adapter,
new Set([this.id])
);
}

public emit<Ev extends EventNames<EmitEvents>>(
Expand Down
6 changes: 3 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}
Expand Down
64 changes: 38 additions & 26 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ export class Namespace<
* @return self
* @public
*/
public to(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
return new BroadcastOperator(this.adapter).to(room);
public to(room: Room | Room[]) {
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).to(room);
}

/**
Expand All @@ -186,8 +186,8 @@ export class Namespace<
* @return self
* @public
*/
public in(room: Room | Room[]): BroadcastOperator<EmitEvents, SocketData> {
return new BroadcastOperator(this.adapter).in(room);
public in(room: Room | Room[]) {
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).in(room);
}

/**
Expand All @@ -197,10 +197,10 @@ export class Namespace<
* @return self
* @public
*/
public except(
room: Room | Room[]
): BroadcastOperator<EmitEvents, SocketData> {
return new BroadcastOperator(this.adapter).except(room);
public except(room: Room | Room[]) {
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).except(
room
);
}

/**
Expand Down Expand Up @@ -345,7 +345,9 @@ export class Namespace<
* @public
*/
public allSockets(): Promise<Set<SocketId>> {
return new BroadcastOperator(this.adapter).allSockets();
return new BroadcastOperator<EmitEvents, SocketData>(
this.adapter
).allSockets();
}

/**
Expand All @@ -355,10 +357,10 @@ export class Namespace<
* @return self
* @public
*/
public compress(
compress: boolean
): BroadcastOperator<EmitEvents, SocketData> {
return new BroadcastOperator(this.adapter).compress(compress);
public compress(compress: boolean) {
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).compress(
compress
);
}

/**
Expand All @@ -369,8 +371,8 @@ export class Namespace<
* @return self
* @public
*/
public get volatile(): BroadcastOperator<EmitEvents, SocketData> {
return new BroadcastOperator(this.adapter).volatile;
public get volatile() {
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).volatile;
}

/**
Expand All @@ -379,8 +381,8 @@ export class Namespace<
* @return self
* @public
*/
public get local(): BroadcastOperator<EmitEvents, SocketData> {
return new BroadcastOperator(this.adapter).local;
public get local() {
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).local;
}

/**
Expand All @@ -397,16 +399,20 @@ export class Namespace<
* @param timeout
*/
public timeout(timeout: number) {
return new BroadcastOperator(this.adapter).timeout(timeout);
return new BroadcastOperator<EmitEvents, SocketData>(this.adapter).timeout(
timeout
);
}

/**
* Returns the matching socket instances
*
* @public
*/
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
return new BroadcastOperator(this.adapter).fetchSockets();
public fetchSockets() {
return new BroadcastOperator<EmitEvents, SocketData>(
this.adapter
).fetchSockets();
}

/**
Expand All @@ -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<EmitEvents, SocketData>(
this.adapter
).socketsJoin(room);
}

/**
Expand All @@ -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<EmitEvents, SocketData>(
this.adapter
).socketsLeave(room);
}

/**
Expand All @@ -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<EmitEvents, SocketData>(
this.adapter
).disconnectSockets(close);
}
}
4 changes: 2 additions & 2 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,10 @@ export class Socket<
}
}

private newBroadcastOperator(): BroadcastOperator<EmitEvents, SocketData> {
private newBroadcastOperator() {
const flags = Object.assign({}, this.flags);
this.flags = {};
return new BroadcastOperator(
return new BroadcastOperator<EmitEvents, SocketData>(
this.adapter,
new Set<Room>(),
new Set<Room>([this.id]),
Expand Down
10 changes: 9 additions & 1 deletion test/socket.io.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ describe("server", () => {
const srv = createServer();
const sio = new Server<ClientToServerEvents, ServerToClientEvents>(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();
Expand All @@ -220,6 +224,10 @@ describe("server", () => {
const srv = createServer();
const sio = new Server<ClientToServerEvents, ServerToClientEvents>(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"));
Expand All @@ -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;
}
Expand Down

0 comments on commit e357daf

Please sign in to comment.