Skip to content

Commit 7a09af0

Browse files
authored
Merge pull request #242 from boostcampwm-2024/feature-socket-fix
[Feature-socket-fix] ์†Œ์ผ“์ด ์ž˜๋ชป๋œ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์—†์•  ์ด๋ฒคํŠธ ์ˆ˜์‹  ๋ฐ ๋ฐ˜์˜์ด ์•ˆ ๋˜๋˜ ๋ฌธ์ œ ํ•ด๊ฒฐ
2 parents e79599b + 9ed721a commit 7a09af0

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

โ€Žclient/src/store/NodeListProvider.tsxโ€Ž

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -56,50 +56,52 @@ export default function NodeListProvider({ children }: { children: ReactNode })
5656
const handleSocketEvent = useConnectionStore((state) => state.handleSocketEvent);
5757

5858
useEffect(() => {
59-
socket?.on("joinRoom", (initialData) => {
60-
updateLoadingStatus({ type: "socketLoading", status: true });
61-
setTimeout(() => {
62-
setData({ ...initialData.nodeData });
63-
overrideHistory(JSON.stringify(initialData.nodeData));
64-
initializeTitle(initialData);
65-
initializeContent(initialData);
66-
initializeAiCount(initialData);
67-
updateLoadingStatus({ type: "socketLoading", status: false });
68-
}, 0);
69-
});
70-
71-
socket?.on("updateNode", (updatedNodeData) => {
72-
overrideNodeData(updatedNodeData);
73-
});
74-
75-
socket?.on("updateTitle", (updatedTitle) => {
76-
updateTitle(updatedTitle.title);
77-
});
78-
79-
socket?.on("updateContent", (updatedContent) => {
80-
updateContent(updatedContent.content);
81-
});
82-
83-
socket?.on("disconnect", () => {
84-
setData({});
85-
overrideHistory(JSON.stringify({}));
86-
});
87-
88-
socket?.on("aiPending", (response) => {
89-
updateLoadingStatus({ type: "aiPending", status: response.status });
90-
});
59+
if (!socket) return;
60+
61+
const eventHandlers = {
62+
joinRoom: (initialData) => {
63+
updateLoadingStatus({ type: "socketLoading", status: true });
64+
setTimeout(() => {
65+
setData({ ...initialData.nodeData });
66+
overrideHistory(JSON.stringify(initialData.nodeData));
67+
initializeTitle(initialData);
68+
initializeContent(initialData);
69+
initializeAiCount(initialData);
70+
updateLoadingStatus({ type: "socketLoading", status: false });
71+
}, 0);
72+
},
73+
disconnect: () => {
74+
setData({});
75+
overrideHistory(JSON.stringify({}));
76+
},
77+
aiPending: (response) => {
78+
updateLoadingStatus({ type: "aiPending", status: response.status });
79+
},
80+
aiResponse: (response) => {
81+
decreaseAiCount();
82+
const initializedNodes = initializeNodePosition(response);
83+
handleSocketEvent({
84+
actionType: "updateNode",
85+
payload: initializedNodes,
86+
callback: (response) => {
87+
overrideNodeData(response);
88+
overrideHistory(JSON.stringify(response));
89+
},
90+
});
91+
},
92+
updateNode: (updatedNodeData) => {
93+
setData(updatedNodeData);
94+
},
95+
updateTitle: (updatedTitle) => {
96+
updateTitle(updatedTitle.title);
97+
},
98+
updateContent: (updatedContent) => {
99+
updateContent(updatedContent.content);
100+
},
101+
};
91102

92-
socket?.on("aiResponse", (response) => {
93-
decreaseAiCount();
94-
const initializedNodes = initializeNodePosition(response);
95-
handleSocketEvent({
96-
actionType: "updateNode",
97-
payload: initializedNodes,
98-
callback: (response) => {
99-
overrideNodeData(response);
100-
overrideHistory(JSON.stringify(response));
101-
},
102-
});
103+
Object.entries(eventHandlers).forEach(([event, handler]) => {
104+
socket.on(event, handler);
103105
});
104106

105107
return () => {

โ€Žclient/src/store/createSocketSlice.tsโ€Ž

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ export const createSocketSlice: StateCreator<ConnectionStore, [], [], SocketSlic
8787
handleSocketEvent: ({ actionType, payload, callback }: HandleSocketEventProps) => {
8888
const socket = get().socket;
8989
if (!socket) return;
90-
91-
socket.off(actionType);
92-
socket.emit(actionType, payload);
93-
94-
socket.on(actionType, (response) => {
90+
const handler = (response) => {
9591
if (response && callback) callback(response);
9692
set({ currentJobStatus: "success" });
97-
});
93+
socket.off(actionType, handler);
94+
};
95+
socket.on(actionType, handler);
96+
socket.emit(actionType, payload);
9897
},
9998

10099
handleConnection: async () => {

0 commit comments

Comments
ย (0)