Skip to content

Commit 5b79fb7

Browse files
authored
Merge pull request #341 from avinxshKD/fix/graph-dispose-leak
fix: dispose cy instance and autosave timer on tab close
2 parents 3d52392 + d7f15fe commit 5b79fb7

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/GraphArea.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ function Graph({
5757

5858
useEffect(() => {
5959
const handleResize = () => setContainerDim(ref.current);
60+
let graph = null;
6061
if (ref.current) {
6162
setContainerDim(ref.current);
6263
window.addEventListener('resize', handleResize);
63-
setInstance(initialiseNewGraph());
64+
graph = initialiseNewGraph();
65+
setInstance(graph);
6466
}
65-
return () => window.removeEventListener('resize', handleResize);
67+
return () => {
68+
window.removeEventListener('resize', handleResize);
69+
if (graph) graph.dispose();
70+
};
6671
}, [ref]);
6772

6873
// Update theme when darkMode changes

src/component/TabBar.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const TabBar = ({ superState, dispatcher }) => {
1515
const [tabToClose, setTabToClose] = useState(null);
1616

1717
const closeTab = (i) => {
18-
localStorageManager.remove(superState.graphs[i] ? superState.graphs[i].graphID : null);
18+
const graph = superState.graphs[i];
19+
if (graph && graph.instance) graph.instance.dispose();
20+
localStorageManager.remove(graph ? graph.graphID : null);
1921
dispatcher({ type: T.REMOVE_GRAPH, payload: i });
2022
if (!superState.curGraphIndex && superState.graphs.length === 1) {
2123
dispatcher({ type: T.SET_CUR_INSTANCE, payload: null });

src/graph-builder/graph-core/1-core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ class CoreGraph {
372372
}
373373
}
374374

375+
dispose() {
376+
if (!this.cy) return;
377+
if (this.autoSaveIntervalId !== null) clearTimeout(this.autoSaveIntervalId);
378+
this.autoSaveIntervalId = null;
379+
this.cy.destroy();
380+
this.cy = null;
381+
this.dispatcher = null;
382+
}
383+
375384
reset() {
376385
this.resetAllComp();
377386
this.resetAllAction();

0 commit comments

Comments
 (0)