Skip to content

Commit ad93de6

Browse files
Copilotmikebarkmin
andcommitted
Fix infinite loop in LearningMap onChange and complete implementation
Co-authored-by: mikebarkmin <2592379+mikebarkmin@users.noreply.github.com>
1 parent 20736cc commit ad93de6

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

packages/learningmap/src/LearningMap.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function LearningMap({
124124
root.dispatchEvent(new CustomEvent("change", { detail: minimalState }));
125125
}
126126
}
127-
}, [nodes, onChange]);
127+
}, [nodes, getViewport, getRoadmapState]);
128128

129129
const defaultEdgeOptions = {
130130
animated: false,

platforms/web/src/Learn.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useState, useCallback, useRef } from 'react';
22
import { useNavigate, useLocation } from 'react-router-dom';
33
import { LearningMap } from '@learningmap/learningmap';
44
import type { RoadmapState } from '@learningmap/learningmap';
@@ -12,6 +12,7 @@ function Learn() {
1212
const [loading, setLoading] = useState(false);
1313
const [error, setError] = useState<string | null>(null);
1414
const [newMapUrl, setNewMapUrl] = useState('');
15+
const updateTimeoutRef = useRef<number | null>(null);
1516

1617
const {
1718
addLearningMap,
@@ -64,11 +65,17 @@ function Learn() {
6465
});
6566
}, [jsonId, getLearningMap, addLearningMap]);
6667

67-
const handleStateChange = (state: RoadmapState) => {
68+
const handleStateChange = useCallback((state: RoadmapState) => {
6869
if (jsonId) {
69-
updateState(jsonId, state);
70+
// Debounce state updates to prevent infinite loops
71+
if (updateTimeoutRef.current) {
72+
clearTimeout(updateTimeoutRef.current);
73+
}
74+
updateTimeoutRef.current = setTimeout(() => {
75+
updateState(jsonId, state);
76+
}, 500);
7077
}
71-
};
78+
}, [jsonId, updateState]);
7279

7380
const handleAddMap = () => {
7481
// Parse URL to extract json ID
@@ -120,6 +127,7 @@ function Learn() {
120127
);
121128
}
122129

130+
123131
return (
124132
<div className="learn-container">
125133
<div className="learn-header">
@@ -129,6 +137,7 @@ function Learn() {
129137
<h1>{learningMap.roadmapData.settings?.title || 'Learning Map'}</h1>
130138
</div>
131139
<LearningMap
140+
key={jsonId}
132141
roadmapData={learningMap.roadmapData}
133142
initialState={learningMap.state}
134143
onChange={handleStateChange}

0 commit comments

Comments
 (0)