Refactor ObjectDisplay and ObjectTable components for improved subscription 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 ObjectDisplay to subscribe to object updates based on the object ID, enhancing responsiveness to changes. - Refactored ObjectTable to streamline subscription logic, ensuring real-time updates are managed more efficiently and reducing unnecessary re-renders.
This commit is contained in:
parent
1a68456be6
commit
c6ec643a2f
@ -86,12 +86,18 @@ const ObjectDisplay = ({
|
||||
[fetchSpotlightData, isMinimalObject, objectType]
|
||||
)
|
||||
|
||||
// Subscribe to object updates when component mounts
|
||||
const subscriptionObjectId = getStringId(object)
|
||||
|
||||
// Subscribe to object updates when the object ID changes
|
||||
useEffect(() => {
|
||||
const id = getStringId(object)
|
||||
if (isValidId(id) && objectType && connected && token != null) {
|
||||
if (
|
||||
isValidId(subscriptionObjectId) &&
|
||||
objectType &&
|
||||
connected &&
|
||||
token != null
|
||||
) {
|
||||
const objectUpdatesUnsubscribe = subscribeToObjectUpdates(
|
||||
id,
|
||||
subscriptionObjectId,
|
||||
objectType,
|
||||
updateObjectEventHandler
|
||||
)
|
||||
@ -101,14 +107,13 @@ const ObjectDisplay = ({
|
||||
}
|
||||
}
|
||||
}, [
|
||||
object,
|
||||
subscriptionObjectId,
|
||||
objectType,
|
||||
subscribeToObjectUpdates,
|
||||
connected,
|
||||
token,
|
||||
updateObjectEventHandler,
|
||||
isValidId,
|
||||
getStringId
|
||||
isValidId
|
||||
])
|
||||
|
||||
// Update local state when object prop changes
|
||||
|
||||
@ -183,6 +183,12 @@ const ObjectTable = forwardRef(
|
||||
const unsubscribesRef = useRef([])
|
||||
const updateEventHandlerRef = useRef()
|
||||
const subscribeToObjectTypeUpdatesRef = useRef(null)
|
||||
const subscribedTypeRef = useRef(null)
|
||||
const newEventHandlerRef = useRef()
|
||||
const subscriptionFilterRef = useRef()
|
||||
const subscribeToObjectTypeUpdatesFnRef = useRef(
|
||||
subscribeToObjectTypeUpdates
|
||||
)
|
||||
const prevValuesRef = useRef({ type, masterFilter })
|
||||
|
||||
const rowActions =
|
||||
@ -611,6 +617,10 @@ const ObjectTable = forwardRef(
|
||||
return { ...active, ...masterFilter }
|
||||
}, [sidebarFilter, masterFilter])
|
||||
|
||||
newEventHandlerRef.current = newEventHandler
|
||||
subscriptionFilterRef.current = subscriptionFilter
|
||||
subscribeToObjectTypeUpdatesFnRef.current = subscribeToObjectTypeUpdates
|
||||
|
||||
// Subscribe to real-time updates for all items
|
||||
useEffect(() => {
|
||||
if (pages.length > 0 && connected == true) {
|
||||
@ -682,28 +692,26 @@ const ObjectTable = forwardRef(
|
||||
}, [connected])
|
||||
|
||||
useEffect(() => {
|
||||
if (connected == true) {
|
||||
const unsubscribe = subscribeToObjectTypeUpdates(
|
||||
type,
|
||||
subscriptionFilter,
|
||||
newEventHandler
|
||||
)
|
||||
subscribeToObjectTypeUpdatesRef.current = unsubscribe
|
||||
if (connected !== true) return
|
||||
if (subscribedTypeRef.current === type) return
|
||||
|
||||
return () => {
|
||||
if (unsubscribe) unsubscribe()
|
||||
if (subscribeToObjectTypeUpdatesRef.current === unsubscribe) {
|
||||
subscribeToObjectTypeUpdatesRef.current = null
|
||||
}
|
||||
const unsubscribe = subscribeToObjectTypeUpdatesFnRef.current(
|
||||
type,
|
||||
subscriptionFilterRef.current,
|
||||
(params) => newEventHandlerRef.current(params)
|
||||
)
|
||||
subscribeToObjectTypeUpdatesRef.current = unsubscribe
|
||||
subscribedTypeRef.current = type
|
||||
return () => {
|
||||
if (unsubscribe) unsubscribe()
|
||||
if (subscribeToObjectTypeUpdatesRef.current === unsubscribe) {
|
||||
subscribeToObjectTypeUpdatesRef.current = null
|
||||
}
|
||||
if (subscribedTypeRef.current === type) {
|
||||
subscribedTypeRef.current = null
|
||||
}
|
||||
}
|
||||
}, [
|
||||
type,
|
||||
subscriptionFilter,
|
||||
subscribeToObjectTypeUpdates,
|
||||
connected,
|
||||
newEventHandler
|
||||
])
|
||||
}, [type, connected])
|
||||
|
||||
const updateData = useCallback(
|
||||
(id, updatedData) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user