Skip to content

Commit

Permalink
chore(types): make RemoteSocket.data type safe
Browse files Browse the repository at this point in the history
  • Loading branch information
OrkhanAlikhanov committed Jan 3, 2022
1 parent 51784d0 commit 6575691
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions lib/broadcast-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class BroadcastOperator<EmitEvents extends EventsMap>
*
* @public
*/
public fetchSockets(): Promise<RemoteSocket<EmitEvents>[]> {
public fetchSockets<SocketData = any>(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
return this.adapter
.fetchSockets({
rooms: this.rooms,
Expand All @@ -187,9 +187,9 @@ export class BroadcastOperator<EmitEvents extends EventsMap>
return sockets.map((socket) => {
if (socket instanceof Socket) {
// FIXME the TypeScript compiler complains about missing private properties
return socket as unknown as RemoteSocket<EmitEvents>;
return socket as unknown as RemoteSocket<EmitEvents, SocketData>;
} else {
return new RemoteSocket(this.adapter, socket as SocketDetails);
return new RemoteSocket(this.adapter, socket as SocketDetails<SocketData>);
}
});
});
Expand Down Expand Up @@ -247,27 +247,27 @@ export class BroadcastOperator<EmitEvents extends EventsMap>
/**
* Format of the data when the Socket instance exists on another Socket.IO server
*/
interface SocketDetails {
interface SocketDetails<SocketData> {
id: SocketId;
handshake: Handshake;
rooms: Room[];
data: any;
data: SocketData;
}

/**
* Expose of subset of the attributes and methods of the Socket class
*/
export class RemoteSocket<EmitEvents extends EventsMap>
export class RemoteSocket<EmitEvents extends EventsMap, SocketData>
implements TypedEventBroadcaster<EmitEvents>
{
public readonly id: SocketId;
public readonly handshake: Handshake;
public readonly rooms: Set<Room>;
public readonly data: any;
public readonly data: SocketData;

private readonly operator: BroadcastOperator<EmitEvents>;

constructor(adapter: Adapter, details: SocketDetails) {
constructor(adapter: Adapter, details: SocketDetails<SocketData>) {
this.id = details.id;
this.handshake = details.handshake;
this.rooms = new Set(details.rooms);
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ export class Server<
*
* @public
*/
public fetchSockets(): Promise<RemoteSocket<EmitEvents>[]> {
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
return this.sockets.fetchSockets();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export class Namespace<
*
* @public
*/
public fetchSockets(): Promise<RemoteSocket<EmitEvents>[]> {
public fetchSockets(): Promise<RemoteSocket<EmitEvents, SocketData>[]> {
return new BroadcastOperator(this.adapter).fetchSockets();
}

Expand Down

0 comments on commit 6575691

Please sign in to comment.