Skip to content

Commit 966436a

Browse files
committed
PIX-62 introduced typesafety in socket functions
1 parent 4170e3e commit 966436a

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

client/src/services/socket.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class SocketService {
3939
this.socket?.emit('JOIN_LOBBY', lobbyName);
4040
}
4141

42-
onLobbyUsers(cb: (users: any[]) => void) {
42+
onLobbyUsers<T>(cb: (users: T[]) => void) {
4343
this.socket?.on('LOBBY_USERS', cb);
4444
}
4545

46-
onUserJoined(cb: (user: any) => void) {
46+
onUserJoined<T>(cb: (user: T) => void) {
4747
this.socket?.on('USER_JOINED', cb);
4848
}
4949

50-
onUserLeft(cb: (user: any) => void) {
50+
onUserLeft<T>(cb: (user: T) => void) {
5151
this.socket?.on('USER_LEFT', cb);
5252
}
5353

client/src/stores/lobby.store.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ export const useLobbyStore = defineStore('lobby', () => {
2626
});
2727

2828
// Listen for initial user list
29-
socketService.onLobbyUsers((userList: User[]) => {
29+
socketService.onLobbyUsers<User>((userList) => {
3030
users.value = userList;
3131
});
3232

3333
// Listen for new user joining
34-
socketService.onUserJoined((user: User) => {
34+
// Listen for new user joining
35+
socketService.onUserJoined<User>((user) => {
3536
// Avoid duplicates
3637
if (!users.value.find(u => u.id === user.id)) {
3738
users.value.push(user);
3839
}
3940
});
4041

4142
// Listen for user leaving
42-
socketService.onUserLeft((user: User) => {
43+
// Listen for user leaving
44+
socketService.onUserLeft<User>((user) => {
4345
users.value = users.value.filter(u => u.id !== user.id);
4446
});
4547
};

0 commit comments

Comments
 (0)