Compare commits

..

No commits in common. "caa50709ddf97f97fdeda6ebe20bc45bb98abd98" and "5680b067a8116b80dd8e779b282c0cb697bdc226" have entirely different histories.

3 changed files with 38 additions and 70 deletions

View File

@ -82,14 +82,10 @@ const ObjectTable = forwardRef(
// Table state // Table state
const [pages, setPages] = useState([]) const [pages, setPages] = useState([])
const pagesRef = useRef(pages)
const [hasMore, setHasMore] = useState(true) const [hasMore, setHasMore] = useState(true)
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [lazyLoading, setLazyLoading] = useState(false) const [lazyLoading, setLazyLoading] = useState(false)
const [subscribedIds, setSubscribedIds] = useState([])
const [typeSubscribed, setTypeSubscribed] = useState(false)
const rowActions = const rowActions =
model.actions?.filter((action) => action.row == true) || [] model.actions?.filter((action) => action.row == true) || []
@ -144,7 +140,6 @@ const ObjectTable = forwardRef(
order: sorter.order order: sorter.order
}) })
} }
console.log('Fetching data...')
try { try {
const result = await fetchObjects(type, { const result = await fetchObjects(type, {
page: pageNum, page: pageNum,
@ -278,27 +273,25 @@ const ObjectTable = forwardRef(
const reload = useCallback(async () => { const reload = useCallback(async () => {
setLazyLoading(true) setLazyLoading(true)
console.log('Pages during reload', pagesRef.current) for (let i = 0; i < pages.length; i++) {
for (let i = 0; i < pagesRef.current.length; i++) { const page = pages[i]
const page = pagesRef.current[i]
await fetchData(page.pageNum) await fetchData(page.pageNum)
} }
}, [fetchData]) }, [fetchData, pages])
// Update event handler for real-time updates // Update event handler for real-time updates
const updateEventHandler = useCallback((id, updatedData) => { const updateEventHandler = useCallback((updatedData) => {
setPages((prevPages) => setPages((prevPages) =>
prevPages.map((page) => ({ prevPages.map((page) => ({
...page, ...page,
items: page.items.map((item) => { items: page.items.map((item) =>
return item._id === id ? merge({}, item, updatedData) : item item._id === updatedData._id ? merge({}, item, updatedData) : item
}) )
})) }))
) )
}, []) }, [])
const newEventHandler = useCallback(() => { const newEventHandler = useCallback(() => {
console.log('GOT NEW EVENT')
reload() reload()
}, [reload]) }, [reload])
@ -306,19 +299,17 @@ const ObjectTable = forwardRef(
useEffect(() => { useEffect(() => {
if (pages.length > 0 && connected) { if (pages.length > 0 && connected) {
const unsubscribes = [] const unsubscribes = []
// Subscribe to each item in all pages // Subscribe to each item in all pages
pages.forEach((page) => { pages.forEach((page) => {
if (page?.items && page?.items?.length > 0) { if (page?.items && page?.items?.length > 0) {
page.items.forEach((item) => { page.items.forEach((item) => {
if (!item.isSkeleton && !subscribedIds.includes(item?._id)) { if (!item.isSkeleton) {
const unsubscribe = subscribeToObjectUpdates( const unsubscribe = subscribeToObjectUpdates(
item._id, item._id,
type, type,
(updateData) => { updateEventHandler
updateEventHandler(item._id, updateData)
}
) )
setSubscribedIds((prev) => [...prev, item._id])
if (unsubscribe) { if (unsubscribe) {
unsubscribes.push(unsubscribe) unsubscribes.push(unsubscribe)
} }
@ -334,32 +325,18 @@ const ObjectTable = forwardRef(
}) })
} }
} }
}, [ }, [pages, type, subscribeToObjectUpdates, updateEventHandler, connected])
pages,
type,
subscribeToObjectUpdates,
updateEventHandler,
connected,
subscribedIds
])
useEffect(() => { useEffect(() => {
if (connected == true && typeSubscribed == false) { if (connected == true) {
const unsubscribe = subscribeToObjectTypeUpdates(type, newEventHandler) const unsubscribe = subscribeToObjectTypeUpdates(type, newEventHandler)
setTypeSubscribed(true)
return () => { return () => {
if (unsubscribe && typeSubscribed == true) { if (unsubscribe) {
unsubscribe() unsubscribe()
} }
} }
} }
}, [ }, [type, subscribeToObjectTypeUpdates, connected, newEventHandler])
type,
subscribeToObjectTypeUpdates,
connected,
newEventHandler,
typeSubscribed
])
const updateData = useCallback( const updateData = useCallback(
(_id, updatedData) => { (_id, updatedData) => {
@ -474,10 +451,6 @@ const ObjectTable = forwardRef(
} }
] ]
useEffect(() => {
console.log('Pages', pages)
pagesRef.current = pages
}, [pages])
// Add columns in the order specified by model.columns // Add columns in the order specified by model.columns
model.columns.forEach((colName) => { model.columns.forEach((colName) => {
const prop = modelProperties.find((p) => p.name === colName) const prop = modelProperties.find((p) => p.name === colName)

View File

@ -1,4 +1,4 @@
import { useState, useContext, useEffect, useRef, useCallback } from 'react' import { useState, useContext, useEffect, useRef } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { Flex, Alert, Card, Spin, Splitter, Button, Modal, Input } from 'antd' import { Flex, Alert, Card, Spin, Splitter, Button, Modal, Input } from 'antd'
import { LoadingOutlined } from '@ant-design/icons' import { LoadingOutlined } from '@ant-design/icons'
@ -44,8 +44,7 @@ const TemplateEditor = ({
} }
} }
const reloadPreview = useCallback( function reloadPreview(content, testObject = {}, scale = 1) {
(content, testObject = {}, scale = 1) => {
fetchTemplatePreview( fetchTemplatePreview(
objectData._id, objectData._id,
content, content,
@ -63,9 +62,7 @@ const TemplateEditor = ({
} }
} }
) )
}, }
[fetchTemplatePreview, objectData?._id]
)
// Move useEffect to component level and use state to track objectData changes // Move useEffect to component level and use state to track objectData changes
useEffect(() => { useEffect(() => {
@ -73,7 +70,7 @@ const TemplateEditor = ({
console.log('PreviewScale', previewScale) console.log('PreviewScale', previewScale)
reloadPreview(objectData.content, objectData.testObject, previewScale) reloadPreview(objectData.content, objectData.testObject, previewScale)
} }
}, [objectData, previewScale, reloadPreview]) }, [objectData, previewScale])
return ( return (
<> <>

View File

@ -243,17 +243,16 @@ const ApiServerProvider = ({ children }) => {
const offObjectUpdatesEvent = useCallback((id, objectType, callback) => { const offObjectUpdatesEvent = useCallback((id, objectType, callback) => {
if (socketRef.current && socketRef.current.connected == true) { if (socketRef.current && socketRef.current.connected == true) {
const callbacksRefKey = `${objectType}:${id}`
// Remove callback from the subscribed callbacks map // Remove callback from the subscribed callbacks map
if (subscribedCallbacksRef.current.has(callbacksRefKey)) { if (subscribedCallbacksRef.current.has(id)) {
const callbacks = subscribedCallbacksRef.current const callbacks = subscribedCallbacksRef.current
.get(callbacksRefKey) .get(id)
.filter((cb) => cb !== callback) .filter((cb) => cb !== callback)
if (callbacks.length === 0) { if (callbacks.length === 0) {
subscribedCallbacksRef.current.delete(callbacksRefKey) subscribedCallbacksRef.current.delete(id)
socketRef.current.emit('unsubscribe', { id: id, type: objectType }) socketRef.current.emit('unsubscribe', { id: id, type: objectType })
} else { } else {
subscribedCallbacksRef.current.set(callbacksRefKey, callbacks) subscribedCallbacksRef.current.set(id, callbacks)
} }
} }
} }
@ -262,7 +261,6 @@ const ApiServerProvider = ({ children }) => {
const offObjectTypeUpdatesEvent = useCallback((objectType, callback) => { const offObjectTypeUpdatesEvent = useCallback((objectType, callback) => {
if (socketRef.current && socketRef.current.connected == true) { if (socketRef.current && socketRef.current.connected == true) {
// Remove callback from the subscribed callbacks map // Remove callback from the subscribed callbacks map
console.log('Unsubscribing from type')
if (subscribedCallbacksRef.current.has(objectType)) { if (subscribedCallbacksRef.current.has(objectType)) {
const callbacks = subscribedCallbacksRef.current const callbacks = subscribedCallbacksRef.current
.get(objectType) .get(objectType)