Skip to content

Commit

Permalink
refactor(graph): improve error handling in dispatch function
Browse files Browse the repository at this point in the history
Add try-catch-finally blocks to ensure proper cleanup of persist timer
even when applyActions throws an error. This maintains consistency
between graph state updates and persistence scheduling.

- Wrap graph state updates in try block
- Add error logging and re-throw in catch block
- Move timer cleanup and scheduling to finally block
  • Loading branch information
gentamura committed Feb 10, 2025
1 parent 300e746 commit 775e149
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/contexts/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,22 @@ export function GraphContextProvider({

const dispatch = useCallback(
(actionOrActions: GraphActionOrActions) => {
graphRef.current = applyActions(graphRef.current, actionOrActions);
setGraph(graphRef.current);
try {
graphRef.current = applyActions(graphRef.current, actionOrActions);
setGraph(graphRef.current);

isPendingPersistRef.current = true;
isPendingPersistRef.current = true;
} catch (error) {
console.error("Failed to dispatch actions:", error);

if (persistTimeoutRef.current) {
clearTimeout(persistTimeoutRef.current);
}
throw error;
} finally {
if (persistTimeoutRef.current) {
clearTimeout(persistTimeoutRef.current);
}

persistTimeoutRef.current = setTimeout(persist, 500);
persistTimeoutRef.current = setTimeout(persist, 500);
}
},
[persist],
);
Expand Down

0 comments on commit 775e149

Please sign in to comment.