Enhance SlicerIntegration and ApiServerContext for Improved Connection Handling
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good

- 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.
This commit is contained in:
Tom Butcher 2026-11-09 00:46:57 +00:00
parent f0d4801e2f
commit 5839b6b896
2 changed files with 22 additions and 13 deletions

View File

@ -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(

View File

@ -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
)
}
}
)