Remove unused shouldRender state and related rendering control logic

This commit is contained in:
yangdx
2025-03-19 13:07:16 +08:00
parent 50a8b5fb6d
commit 6aab0eaea2
3 changed files with 13 additions and 30 deletions

View File

@@ -114,7 +114,6 @@ const GraphViewer = () => {
const focusedNode = useGraphStore.use.focusedNode() const focusedNode = useGraphStore.use.focusedNode()
const moveToSelectedNode = useGraphStore.use.moveToSelectedNode() const moveToSelectedNode = useGraphStore.use.moveToSelectedNode()
const isFetching = useGraphStore.use.isFetching() const isFetching = useGraphStore.use.isFetching()
const shouldRender = useGraphStore.use.shouldRender() // Rendering control state
// Get tab visibility // Get tab visibility
const { isTabVisible } = useTabVisibility() const { isTabVisible } = useTabVisibility()
@@ -127,20 +126,19 @@ const GraphViewer = () => {
// Handle component mount/unmount and tab visibility // Handle component mount/unmount and tab visibility
useEffect(() => { useEffect(() => {
// When component mounts or tab becomes visible // When component mounts or tab becomes visible
if (isGraphTabVisible && !shouldRender && !isFetching && !initAttemptedRef.current) { if (isGraphTabVisible && !isFetching && !initAttemptedRef.current) {
// If tab is visible but graph is not rendering, try to enable rendering
useGraphStore.getState().setShouldRender(true)
initAttemptedRef.current = true initAttemptedRef.current = true
console.log('Graph viewer initialized') console.log('GraphViewer is visible')
} }
// Cleanup function when component unmounts
return () => { return () => {
// Only log cleanup, don't actually clean up the WebGL context if (!isGraphTabVisible) {
// This allows the WebGL context to persist across tab switches // Only log cleanup, don't actually clean up the WebGL context
console.log('Graph viewer cleanup') // This allows the WebGL context to persist across tab switches
console.log('GraphViewer is invisible, WebGL context is persisting')
}
} }
}, [isGraphTabVisible, shouldRender, isFetching]) }, [isGraphTabVisible, isFetching])
// Initialize sigma settings once on component mount // Initialize sigma settings once on component mount
// All dynamic settings will be updated in GraphControl using useSetSettings // All dynamic settings will be updated in GraphControl using useSetSettings
@@ -151,13 +149,15 @@ const GraphViewer = () => {
// Clean up sigma instance when component unmounts // Clean up sigma instance when component unmounts
useEffect(() => { useEffect(() => {
return () => { return () => {
// TAB is mount twice in vite dev mode, this is a workaround
const sigma = useGraphStore.getState().sigmaInstance; const sigma = useGraphStore.getState().sigmaInstance;
if (sigma) { if (sigma) {
try { try {
// 销毁sigma实例这会自动清理WebGL上下文 // Destroy sigmaand clear WebGL context
sigma.kill(); sigma.kill();
useGraphStore.getState().setSigmaInstance(null); useGraphStore.getState().setSigmaInstance(null);
console.log('Cleared sigma instance on unmount'); console.log('Cleared sigma instance on Graphviewer unmount');
} catch (error) { } catch (error) {
console.error('Error cleaning up sigma instance:', error); console.error('Error cleaning up sigma instance:', error);
} }

View File

@@ -260,7 +260,6 @@ const useLightrangeGraph = () => {
const state = useGraphStore.getState() const state = useGraphStore.getState()
state.setIsFetching(true) state.setIsFetching(true)
state.setShouldRender(false) // Disable rendering during data loading
// Clear selection and highlighted nodes before fetching new graph // Clear selection and highlighted nodes before fetching new graph
state.clearSelection() state.clearSelection()
@@ -305,8 +304,6 @@ const useLightrangeGraph = () => {
// Reset camera view // Reset camera view
state.setMoveToSelectedNode(true) state.setMoveToSelectedNode(true)
// Enable rendering if the tab is visible
state.setShouldRender(isGraphTabVisible)
state.setIsFetching(false) state.setIsFetching(false)
}).catch((error) => { }).catch((error) => {
console.error('Error fetching graph data:', error) console.error('Error fetching graph data:', error)
@@ -314,7 +311,6 @@ const useLightrangeGraph = () => {
// Reset state on error // Reset state on error
const state = useGraphStore.getState() const state = useGraphStore.getState()
state.setIsFetching(false) state.setIsFetching(false)
state.setShouldRender(isGraphTabVisible)
dataLoadedRef.current = false dataLoadedRef.current = false
fetchInProgressRef.current = false fetchInProgressRef.current = false
state.setGraphDataFetchAttempted(false) state.setGraphDataFetchAttempted(false)
@@ -326,15 +322,7 @@ const useLightrangeGraph = () => {
useEffect(() => { useEffect(() => {
// When tab becomes visible // When tab becomes visible
if (isGraphTabVisible) { if (isGraphTabVisible) {
// If we have data, enable rendering
if (rawGraph) {
useGraphStore.getState().setShouldRender(true)
}
// We no longer reset the fetch attempted flag here to prevent continuous API calls // We no longer reset the fetch attempted flag here to prevent continuous API calls
} else {
// When tab becomes invisible, disable rendering
useGraphStore.getState().setShouldRender(false)
} }
}, [isGraphTabVisible, rawGraph]) }, [isGraphTabVisible, rawGraph])

View File

@@ -74,7 +74,6 @@ interface GraphState {
moveToSelectedNode: boolean moveToSelectedNode: boolean
isFetching: boolean isFetching: boolean
shouldRender: boolean
// Global flags to track data fetching attempts // Global flags to track data fetching attempts
graphDataFetchAttempted: boolean graphDataFetchAttempted: boolean
@@ -95,7 +94,6 @@ interface GraphState {
setAllDatabaseLabels: (labels: string[]) => void setAllDatabaseLabels: (labels: string[]) => void
fetchAllDatabaseLabels: () => Promise<void> fetchAllDatabaseLabels: () => Promise<void>
setIsFetching: (isFetching: boolean) => void setIsFetching: (isFetching: boolean) => void
setShouldRender: (shouldRender: boolean) => void
// 搜索引擎方法 // 搜索引擎方法
setSearchEngine: (engine: MiniSearch | null) => void setSearchEngine: (engine: MiniSearch | null) => void
@@ -122,7 +120,6 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
moveToSelectedNode: false, moveToSelectedNode: false,
isFetching: false, isFetching: false,
shouldRender: false,
// Initialize global flags // Initialize global flags
graphDataFetchAttempted: false, graphDataFetchAttempted: false,
@@ -137,7 +134,6 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
setIsFetching: (isFetching: boolean) => set({ isFetching }), setIsFetching: (isFetching: boolean) => set({ isFetching }),
setShouldRender: (shouldRender: boolean) => set({ shouldRender }),
setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) => setSelectedNode: (nodeId: string | null, moveToSelectedNode?: boolean) =>
set({ selectedNode: nodeId, moveToSelectedNode }), set({ selectedNode: nodeId, moveToSelectedNode }),
setFocusedNode: (nodeId: string | null) => set({ focusedNode: nodeId }), setFocusedNode: (nodeId: string | null) => set({ focusedNode: nodeId }),
@@ -159,8 +155,7 @@ const useGraphStoreBase = create<GraphState>()((set) => ({
rawGraph: null, rawGraph: null,
sigmaGraph: null, // to avoid other components from acccessing graph objects sigmaGraph: null, // to avoid other components from acccessing graph objects
searchEngine: null, searchEngine: null,
moveToSelectedNode: false, moveToSelectedNode: false
shouldRender: false
}); });
}, },