added logic to clear old timeout before creating new one (prevents accumulation when nodes are selected rapidly, ex. slideshow)

This commit is contained in:
Vidya Rupak 2025-12-22 15:18:56 -07:00
parent 97599c42ef
commit b645d18342

View file

@ -536,6 +536,7 @@ export const MemoryGraph = ({
// Slideshow logic - simulate actual node clicks with physics
const slideshowIntervalRef = useRef<NodeJS.Timeout | null>(null)
const physicsTimeoutRef = useRef<NodeJS.Timeout | null>(null)
const lastSelectedIndexRef = useRef<number>(-1)
const isSlideshowActiveRef = useRef(isSlideshowActive)
@ -563,11 +564,15 @@ export const MemoryGraph = ({
}, [nodes, handleNodeClick, centerViewportOn, containerSize, onSlideshowNodeChange, forceSimulation])
useEffect(() => {
// Clear any existing interval when isSlideshowActive changes
// Clear any existing interval and timeout when isSlideshowActive changes
if (slideshowIntervalRef.current) {
clearInterval(slideshowIntervalRef.current)
slideshowIntervalRef.current = null
}
if (physicsTimeoutRef.current) {
clearTimeout(physicsTimeoutRef.current)
physicsTimeoutRef.current = null
}
if (!isSlideshowActive) {
// Close the popover when stopping slideshow
@ -611,9 +616,13 @@ export const MemoryGraph = ({
// Trigger physics animation briefly
forceSimulationRef.current.reheat()
// Cool down physics after 1 second
setTimeout(() => {
// Cool down physics after 1 second (cleanup old timeout first)
if (physicsTimeoutRef.current) {
clearTimeout(physicsTimeoutRef.current)
}
physicsTimeoutRef.current = setTimeout(() => {
forceSimulationRef.current.coolDown()
physicsTimeoutRef.current = null
}, 1000)
// Notify parent component
@ -634,6 +643,10 @@ export const MemoryGraph = ({
clearInterval(slideshowIntervalRef.current)
slideshowIntervalRef.current = null
}
if (physicsTimeoutRef.current) {
clearTimeout(physicsTimeoutRef.current)
physicsTimeoutRef.current = null
}
}
}, [isSlideshowActive]) // Only depend on isSlideshowActive