Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(types): make RemoteSocket.data type safe #4234

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/broadcast-operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ 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 +189,12 @@ 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 +252,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>[]> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we would have:

const sockets = await io.fetchSockets();

// with the BroadcastOperator
const sockets = await io.in("room-1").fetchSockets<SocketData>();

Is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. We loss the SocketData type when BroadcastOperator is created, so it has to be specified. Otherwise it will default to any

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I think it would make more sense to go with your first solution here, so we have the same API with and without operator (like to() or except()).

const sockets = await io.fetchSockets();
const sockets = await io.in("room-1").fetchSockets(); // no additional types

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. Will update the PR

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