From 49a07d36d360f54792b059871559ad5606b2bac6 Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Sat, 14 Oct 2023 16:27:44 -0500 Subject: [PATCH 1/2] Don't connect mulitple times --- src/ui.tsx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/ui.tsx b/src/ui.tsx index d98d964..d824dfc 100644 --- a/src/ui.tsx +++ b/src/ui.tsx @@ -240,7 +240,7 @@ class SaxiDriver implements Driver { const websocketProtocol = document.location.protocol === "https:" ? "wss" : "ws"; this.socket = new WebSocket(`${websocketProtocol}://${document.location.host}/chat`); - + this.socket.addEventListener("open", () => { console.log(`Connected to EBB server.`); this.connected = true; @@ -1046,9 +1046,18 @@ function PortSelector({driver, setDriver}: {driver: Driver; setDriver: (d: Drive } function Root() { - const [driver, setDriver] = useState( - IS_WEB ? null as Driver | null : SaxiDriver.connect() - ) + const [driver, setDriver] = useState(null); + const [isDriverConnected, setIsDriverConnected] = useState(false); + useEffect(() => { + if (isDriverConnected) return + if (IS_WEB) { + setDriver(null as Driver); + } else { + setDriver(SaxiDriver.connect()); + } + setIsDriverConnected(true); + }, [isDriverConnected]); + const [state, dispatch] = useReducer(reducer, initialState); const [isPlanning, plan, setPlan] = usePlan(state.paths, state.planOptions); const [isLoadingFile, setIsLoadingFile] = useState(false); @@ -1077,7 +1086,7 @@ function Root() { driver.onplan = (plan: Plan) => { setPlan(plan); }; - }, [driver]) + }, IS_WEB ? [driver] : []) useEffect(() => { const ondrop = (e: DragEvent) => { @@ -1120,7 +1129,7 @@ function Root() { document.body.removeEventListener("dragleave", ondragleave); document.removeEventListener("paste", onpaste); }; - }); + }, []); // Each time new motion is started, save the start time const currentMotionStartedTime = useMemo(() => { From 45dace044aab499cf9561bc6d9aeea4b6cb48d7b Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Sat, 14 Oct 2023 17:15:51 -0500 Subject: [PATCH 2/2] fix connectivity --- src/ui.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui.tsx b/src/ui.tsx index d824dfc..5e37d85 100644 --- a/src/ui.tsx +++ b/src/ui.tsx @@ -1086,7 +1086,7 @@ function Root() { driver.onplan = (plan: Plan) => { setPlan(plan); }; - }, IS_WEB ? [driver] : []) + }, [driver]) useEffect(() => { const ondrop = (e: DragEvent) => {