Enhance SlicerIntegration and ApiServerContext for Improved Connection Handling
All checks were successful
farmcontrol/farmcontrol-ui/pipeline/head This commit looks good
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:
parent
f0d4801e2f
commit
5839b6b896
@ -18,7 +18,7 @@ const { Title } = Typography
|
|||||||
const SlicerIntegration = () => {
|
const SlicerIntegration = () => {
|
||||||
const location = useLocation()
|
const location = useLocation()
|
||||||
const printerId = new URLSearchParams(location.search).get('printerId')
|
const printerId = new URLSearchParams(location.search).get('printerId')
|
||||||
const { updateObject, subscribeToObjectUpdates } =
|
const { connected, updateObject, subscribeToObjectUpdates } =
|
||||||
useContext(ApiServerContext)
|
useContext(ApiServerContext)
|
||||||
const receivedPendingUploadsUpdateRef = useRef(false)
|
const receivedPendingUploadsUpdateRef = useRef(false)
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ const SlicerIntegration = () => {
|
|||||||
}, [objectFormState.objectData?.pendingSlicerUploads])
|
}, [objectFormState.objectData?.pendingSlicerUploads])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!printerId) {
|
if (!printerId || connected !== true) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,14 +70,18 @@ const SlicerIntegration = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
receivedPendingUploadsUpdateRef.current = true
|
receivedPendingUploadsUpdateRef.current = true
|
||||||
setPendingSlicerUploads(object.pendingSlicerUploads || [])
|
setPendingSlicerUploads(
|
||||||
|
Array.isArray(object.pendingSlicerUploads)
|
||||||
|
? object.pendingSlicerUploads
|
||||||
|
: []
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (unsubscribe) unsubscribe()
|
if (unsubscribe) unsubscribe()
|
||||||
}
|
}
|
||||||
}, [printerId, subscribeToObjectUpdates])
|
}, [connected, printerId, subscribeToObjectUpdates])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const nextUpload = pendingSlicerUploads.find(
|
const nextUpload = pendingSlicerUploads.find(
|
||||||
|
|||||||
@ -1000,7 +1000,7 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
|
|
||||||
const handleObjectUpdate = async (data) => {
|
const handleObjectUpdate = async (data) => {
|
||||||
logger.debug('Notifying object update:', 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 objectType = data.objectType
|
||||||
|
|
||||||
const callbacksRefKey = `${objectType}:${id}`
|
const callbacksRefKey = `${objectType}:${id}`
|
||||||
@ -1176,7 +1176,8 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const offObjectUpdatesEvent = useCallback((id, objectType, callback) => {
|
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)) {
|
if (!subscribedObjectUpdateCallbacksRef.current.has(callbacksRefKey)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1195,7 +1196,7 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
'unsubscribing from object update...'
|
'unsubscribing from object update...'
|
||||||
)
|
)
|
||||||
socketRef.current.emit('unsubscribeObjectUpdate', {
|
socketRef.current.emit('unsubscribeObjectUpdate', {
|
||||||
_id: id,
|
_id: normalizedId,
|
||||||
objectType
|
objectType
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -1258,7 +1259,8 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const callbacksRefKey = `${objectType}:${id}`
|
const normalizedId = String(id).toLowerCase()
|
||||||
|
const callbacksRefKey = `${objectType}:${normalizedId}`
|
||||||
if (!subscribedObjectUpdateCallbacksRef.current.has(callbacksRefKey)) {
|
if (!subscribedObjectUpdateCallbacksRef.current.has(callbacksRefKey)) {
|
||||||
subscribedObjectUpdateCallbacksRef.current.set(callbacksRefKey, [])
|
subscribedObjectUpdateCallbacksRef.current.set(callbacksRefKey, [])
|
||||||
}
|
}
|
||||||
@ -1272,18 +1274,21 @@ const ApiServerProvider = ({ children }) => {
|
|||||||
socket?.connected &&
|
socket?.connected &&
|
||||||
!subscribedObjectUpdateServerRef.current.has(callbacksRefKey)
|
!subscribedObjectUpdateServerRef.current.has(callbacksRefKey)
|
||||||
) {
|
) {
|
||||||
subscribedObjectUpdateServerRef.current.add(callbacksRefKey)
|
|
||||||
socket.emit(
|
socket.emit(
|
||||||
'subscribeToObjectUpdate',
|
'subscribeToObjectUpdate',
|
||||||
{
|
{
|
||||||
_id: id,
|
_id: normalizedId,
|
||||||
objectType
|
objectType
|
||||||
},
|
},
|
||||||
(result) => {
|
(result) => {
|
||||||
if (result?.success) {
|
if (result?.success) {
|
||||||
logger.info('Subscribed to id:', id, 'objectType:', objectType)
|
subscribedObjectUpdateServerRef.current.add(callbacksRefKey)
|
||||||
} else {
|
logger.info(
|
||||||
subscribedObjectUpdateServerRef.current.delete(callbacksRefKey)
|
'Subscribed to id:',
|
||||||
|
normalizedId,
|
||||||
|
'objectType:',
|
||||||
|
objectType
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user