From aeb5bb4269e3fb5e0aa53d7ec94895da5c9b9c18 Mon Sep 17 00:00:00 2001 From: sofvanh <23138848+sofvanh@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:50:13 +1300 Subject: [PATCH] Elegant handling of 'leave graph' --- backend/src/index.ts | 4 ++-- backend/src/websocket/graph/leaveGraph.ts | 11 +++++++++++ backend/src/websocket/graphHandler.ts | 7 ------- frontend/src/hooks/useGraph.ts | 6 +++++- 4 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 backend/src/websocket/graph/leaveGraph.ts delete mode 100644 backend/src/websocket/graphHandler.ts diff --git a/backend/src/index.ts b/backend/src/index.ts index ee3e1a9..2ee603d 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -4,13 +4,13 @@ import { Server } from 'socket.io'; import config from './config'; import { handleAuthenticate } from './websocket/auth/authenticate'; import { handleLogout } from './websocket/auth/logout'; -import { handleLeaveGraph } from './websocket/graphHandler'; import { handleAddArgument } from './websocket/argumentHandler'; import { handleAddReaction, handleRemoveReaction } from './websocket/reactionHandler'; import { SocketHandler, SocketResponse } from './backendTypes'; import { handleGetGraphs } from './websocket/graph/getGraphs'; import { handleCreateGraph } from './websocket/graph/createGraph'; import { handleJoinGraph } from './websocket/graph/joinGraph'; +import { handleLeaveGraph } from './websocket/graph/leaveGraph'; const app = express(); const server = http.createServer(app); @@ -60,8 +60,8 @@ io.on('connection', (socket) => { socket.on('get graphs', wrapHandler(handleGetGraphs)); socket.on('create graph', wrapHandler(handleCreateGraph)); socket.on('join graph', wrapHandler(handleJoinGraph)); + socket.on('leave graph', wrapHandler(handleLeaveGraph)); // TODO Finish transition - socket.on('leave graph', (graphId: string) => handleLeaveGraph(socket, graphId)); socket.on('add argument', (args, callback) => handleAddArgument(socket, io, args, callback)); socket.on('add reaction', (args, callback) => handleAddReaction(socket, io, args, callback)); socket.on('remove reaction', (args, callback) => handleRemoveReaction(socket, io, args, callback)); diff --git a/backend/src/websocket/graph/leaveGraph.ts b/backend/src/websocket/graph/leaveGraph.ts new file mode 100644 index 0000000..28741b7 --- /dev/null +++ b/backend/src/websocket/graph/leaveGraph.ts @@ -0,0 +1,11 @@ +import { SocketHandler } from "../../backendTypes"; + +interface LeaveGraphData { + graphId: string; +} + +export const handleLeaveGraph: SocketHandler = async (socket, io, { graphId }) => { + console.log(`Socket ${socket.id} leaving graph ${graphId}`); + socket.leave(graphId); + return { success: true }; +} \ No newline at end of file diff --git a/backend/src/websocket/graphHandler.ts b/backend/src/websocket/graphHandler.ts deleted file mode 100644 index dad322a..0000000 --- a/backend/src/websocket/graphHandler.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Socket } from 'socket.io'; - - -export const handleLeaveGraph = async (socket: Socket, graphId: string) => { - console.log(`Socket ${socket.id} leaving graph ${graphId}`); - socket.leave(graphId); -}; \ No newline at end of file diff --git a/frontend/src/hooks/useGraph.ts b/frontend/src/hooks/useGraph.ts index 351c3ee..66689cd 100644 --- a/frontend/src/hooks/useGraph.ts +++ b/frontend/src/hooks/useGraph.ts @@ -59,7 +59,11 @@ export function useGraph(graphId: string) { }); }); return () => { - socket?.emit('leave graph', graphId); + socket?.emit('leave graph', { graphId }, (response: any) => { + if (!response.success) { + console.error('Failed to leave graph:', response.error); + } + }); socket?.off('graph update'); socket?.off('argument added'); socket?.off('user reaction update');