Skip to content

Commit b0e7d46

Browse files
chore(types): make RemoteSocket.data type safe
1 parent 51784d0 commit b0e7d46

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

Diff for: lib/broadcast-operator.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ export class BroadcastOperator<EmitEvents extends EventsMap>
177177
*
178178
* @public
179179
*/
180-
public fetchSockets(): Promise<RemoteSocket<EmitEvents>[]> {
180+
public fetchSockets<SocketData = any>(): Promise<
181+
RemoteSocket<EmitEvents, SocketData>[]
182+
> {
181183
return this.adapter
182184
.fetchSockets({
183185
rooms: this.rooms,
@@ -187,9 +189,12 @@ export class BroadcastOperator<EmitEvents extends EventsMap>
187189
return sockets.map((socket) => {
188190
if (socket instanceof Socket) {
189191
// FIXME the TypeScript compiler complains about missing private properties
190-
return socket as unknown as RemoteSocket<EmitEvents>;
192+
return socket as unknown as RemoteSocket<EmitEvents, SocketData>;
191193
} else {
192-
return new RemoteSocket(this.adapter, socket as SocketDetails);
194+
return new RemoteSocket(
195+
this.adapter,
196+
socket as SocketDetails<SocketData>
197+
);
193198
}
194199
});
195200
});
@@ -247,27 +252,27 @@ export class BroadcastOperator<EmitEvents extends EventsMap>
247252
/**
248253
* Format of the data when the Socket instance exists on another Socket.IO server
249254
*/
250-
interface SocketDetails {
255+
interface SocketDetails<SocketData> {
251256
id: SocketId;
252257
handshake: Handshake;
253258
rooms: Room[];
254-
data: any;
259+
data: SocketData;
255260
}
256261

257262
/**
258263
* Expose of subset of the attributes and methods of the Socket class
259264
*/
260-
export class RemoteSocket<EmitEvents extends EventsMap>
265+
export class RemoteSocket<EmitEvents extends EventsMap, SocketData>
261266
implements TypedEventBroadcaster<EmitEvents>
262267
{
263268
public readonly id: SocketId;
264269
public readonly handshake: Handshake;
265270
public readonly rooms: Set<Room>;
266-
public readonly data: any;
271+
public readonly data: SocketData;
267272

268273
private readonly operator: BroadcastOperator<EmitEvents>;
269274

270-
constructor(adapter: Adapter, details: SocketDetails) {
275+
constructor(adapter: Adapter, details: SocketDetails<SocketData>) {
271276
this.id = details.id;
272277
this.handshake = details.handshake;
273278
this.rooms = new Set(details.rooms);

Diff for: lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ export class Server<
769769
*
770770
* @public
771771
*/
772-
public fetchSockets(): Promise<RemoteSocket<EmitEvents>[]> {
772+
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
773773
return this.sockets.fetchSockets();
774774
}
775775

Diff for: lib/namespace.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ export class Namespace<
377377
*
378378
* @public
379379
*/
380-
public fetchSockets(): Promise<RemoteSocket<EmitEvents>[]> {
380+
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
381381
return new BroadcastOperator(this.adapter).fetchSockets();
382382
}
383383

0 commit comments

Comments
 (0)