Skip to content

Commit 8685041

Browse files
committed
fix error handling
1 parent 538095c commit 8685041

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

src/utils/socket-utils.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ type THandler<E extends TEvent> = RoomsSocketEventsMap["listen"][E];
3333

3434
function initializeSocket(
3535
config: RoomsSocketConfig,
36-
handlers: Partial<{
37-
[k in TEvent]: (
38-
...args: Parameters<RoomsSocketEventsMap["listen"][k]>
39-
) => void;
40-
}>
36+
handlers: Partial<RoomsSocketEventsMap["listen"]>
4137
) {
4238
const socket = io(config.serverUrl, {
4339
path: config.mountPath,
@@ -48,22 +44,22 @@ function initializeSocket(
4844
},
4945
}) as Socket<RoomsSocketEventsMap["listen"], RoomsSocketEventsMap["emit"]>;
5046

51-
socket.on("connect", () => {
47+
socket.on("connect", async () => {
5248
console.log("connect", socket.id);
53-
handlers.connect?.();
49+
return handlers.connect?.();
5450
});
5551

56-
socket.on("update_model", (msg) => {
57-
handlers.update_model?.(msg);
52+
socket.on("update_model", async (msg) => {
53+
return handlers.update_model?.(msg);
5854
});
5955

60-
socket.on("error", (error) => {
61-
handlers.error?.(error);
56+
socket.on("error", async (error) => {
57+
return handlers.error?.(error);
6258
});
6359

64-
socket.on("connect_error", (error) => {
60+
socket.on("connect_error", async (error) => {
6561
console.error("connect_error", error);
66-
handlers.error?.(error);
62+
return handlers.error?.(error);
6763
});
6864

6965
return socket;
@@ -75,10 +71,10 @@ export function RoomsSocket({ config }: { config: RoomsSocketConfig }) {
7571
let currentConfig = { ...config };
7672
const roomsToListeners: Record<
7773
TSocketRoom,
78-
Partial<{ [k in TEvent]: THandler<k> }>[]
74+
Partial<RoomsSocketEventsMap["listen"]>[]
7975
> = {};
8076

81-
const handlers: { [k in TEvent]: THandler<k> } = {
77+
const handlers: RoomsSocketEventsMap["listen"] = {
8278
connect: async () => {
8379
const promises: Promise<void>[] = [];
8480
Object.keys(roomsToListeners).forEach((room) => {
@@ -100,7 +96,10 @@ export function RoomsSocket({ config }: { config: RoomsSocketConfig }) {
10096
},
10197
error: async (error) => {
10298
console.error("error", error);
103-
await handlers.error?.(error);
99+
const promises = Object.values(roomsToListeners)
100+
.flat()
101+
.map((listener) => listener.error?.(error));
102+
await Promise.all(promises);
104103
},
105104
};
106105

@@ -154,9 +153,12 @@ export function RoomsSocket({ config }: { config: RoomsSocketConfig }) {
154153
roomsToListeners[room].push(handlers);
155154

156155
return () => {
157-
roomsToListeners[room] = roomsToListeners[room].filter(
158-
(listener) => listener !== handlers
159-
);
156+
roomsToListeners[room] =
157+
roomsToListeners[room]?.filter((listener) => listener !== handlers) ??
158+
[];
159+
if (roomsToListeners[room].length === 0) {
160+
leaveRoom(room);
161+
}
160162
};
161163
};
162164

0 commit comments

Comments
 (0)