From 5839b6b896d2313163f7379a526beb1e33ecffbd Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Mon, 9 Nov 2026 00:46:57 +0000 Subject: [PATCH] Enhance SlicerIntegration and ApiServerContext for Improved Connection Handling - Updated SlicerIntegration to check for printerId and connection status before proceeding with uploads. - Refactored object ID handling in ApiServerContext to normalize IDs to lowercase, ensuring consistency across updates and subscriptions. - Improved pending uploads state management in SlicerIntegration to handle non-array values gracefully. --- .../Production/Printers/SlicerIntegration.jsx | 12 ++++++---- .../Dashboard/context/ApiServerContext.jsx | 23 +++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/components/Dashboard/Production/Printers/SlicerIntegration.jsx b/src/components/Dashboard/Production/Printers/SlicerIntegration.jsx index e6dd2cf2..1251df10 100644 --- a/src/components/Dashboard/Production/Printers/SlicerIntegration.jsx +++ b/src/components/Dashboard/Production/Printers/SlicerIntegration.jsx @@ -18,7 +18,7 @@ const { Title } = Typography const SlicerIntegration = () => { const location = useLocation() const printerId = new URLSearchParams(location.search).get('printerId') - const { updateObject, subscribeToObjectUpdates } = + const { connected, updateObject, subscribeToObjectUpdates } = useContext(ApiServerContext) const receivedPendingUploadsUpdateRef = useRef(false) @@ -54,7 +54,7 @@ const SlicerIntegration = () => { }, [objectFormState.objectData?.pendingSlicerUploads]) useEffect(() => { - if (!printerId) { + if (!printerId || connected !== true) { return undefined } @@ -70,14 +70,18 @@ const SlicerIntegration = () => { } receivedPendingUploadsUpdateRef.current = true - setPendingSlicerUploads(object.pendingSlicerUploads || []) + setPendingSlicerUploads( + Array.isArray(object.pendingSlicerUploads) + ? object.pendingSlicerUploads + : [] + ) } ) return () => { if (unsubscribe) unsubscribe() } - }, [printerId, subscribeToObjectUpdates]) + }, [connected, printerId, subscribeToObjectUpdates]) useEffect(() => { const nextUpload = pendingSlicerUploads.find( diff --git a/src/components/Dashboard/context/ApiServerContext.jsx b/src/components/Dashboard/context/ApiServerContext.jsx index d5638695..421cec18 100644 --- a/src/components/Dashboard/context/ApiServerContext.jsx +++ b/src/components/Dashboard/context/ApiServerContext.jsx @@ -1000,7 +1000,7 @@ const ApiServerProvider = ({ children }) => { const handleObjectUpdate = async (data) => { logger.debug('Notifying object update:', data) - const id = data._id + const id = data._id == null ? data._id : String(data._id).toLowerCase() const objectType = data.objectType const callbacksRefKey = `${objectType}:${id}` @@ -1176,7 +1176,8 @@ const ApiServerProvider = ({ children }) => { } const offObjectUpdatesEvent = useCallback((id, objectType, callback) => { - const callbacksRefKey = `${objectType}:${id}` + const normalizedId = id == null ? id : String(id).toLowerCase() + const callbacksRefKey = `${objectType}:${normalizedId}` if (!subscribedObjectUpdateCallbacksRef.current.has(callbacksRefKey)) { return } @@ -1195,7 +1196,7 @@ const ApiServerProvider = ({ children }) => { 'unsubscribing from object update...' ) socketRef.current.emit('unsubscribeObjectUpdate', { - _id: id, + _id: normalizedId, objectType }) } @@ -1258,7 +1259,8 @@ const ApiServerProvider = ({ children }) => { return undefined } - const callbacksRefKey = `${objectType}:${id}` + const normalizedId = String(id).toLowerCase() + const callbacksRefKey = `${objectType}:${normalizedId}` if (!subscribedObjectUpdateCallbacksRef.current.has(callbacksRefKey)) { subscribedObjectUpdateCallbacksRef.current.set(callbacksRefKey, []) } @@ -1272,18 +1274,21 @@ const ApiServerProvider = ({ children }) => { socket?.connected && !subscribedObjectUpdateServerRef.current.has(callbacksRefKey) ) { - subscribedObjectUpdateServerRef.current.add(callbacksRefKey) socket.emit( 'subscribeToObjectUpdate', { - _id: id, + _id: normalizedId, objectType }, (result) => { if (result?.success) { - logger.info('Subscribed to id:', id, 'objectType:', objectType) - } else { - subscribedObjectUpdateServerRef.current.delete(callbacksRefKey) + subscribedObjectUpdateServerRef.current.add(callbacksRefKey) + logger.info( + 'Subscribed to id:', + normalizedId, + 'objectType:', + objectType + ) } } )