From 5e572ce5ef294c934b7681a6174b3f4d55289079 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Fri, 11 Aug 2023 02:09:47 -0400 Subject: [PATCH] clean up ws correctly --- frontend/src/util/socket.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/frontend/src/util/socket.tsx b/frontend/src/util/socket.tsx index 15dac933c..274962552 100644 --- a/frontend/src/util/socket.tsx +++ b/frontend/src/util/socket.tsx @@ -64,12 +64,15 @@ export const WebsocketProvider = ({ children }: { children: ReactNode }) => { const invalidate = useInvalidate(); const [ws] = useWebsocket(); + const on_open_fn = () => on_open(ws); + const on_message_fn = (e: MessageEvent) => on_message(e, invalidate); + useEffect(() => { - ws.addEventListener("open", () => on_open(ws)); - ws.addEventListener("message", (e) => on_message(e, invalidate)); + ws.addEventListener("open", on_open_fn); + ws.addEventListener("message", on_message_fn); return () => { - ws.removeEventListener("open", () => on_open(ws)); - ws.removeEventListener("message", (e) => on_message(e, invalidate)); + ws.removeEventListener("open", on_open_fn); + ws.removeEventListener("message", on_message_fn); }; });