Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 93 additions & 16 deletions packages/compass-data-modeling/src/components/diagram-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ const ZOOM_OPTIONS = {
minZoom: 0.25,
};

const REFRESH_DIAGRAM_RATE_MS = 250;

type SelectedItems = NonNullable<DiagramState>['selectedItems'];

const DiagramContent: React.FunctionComponent<{
Expand Down Expand Up @@ -319,6 +321,96 @@ const DiagramContent: React.FunctionComponent<{
[handleNodesConnect]
);

// Throttling mechanism for diagram content updates
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suuuper nit, but I would strongly suggest to move it into its own hook outside of render method just for the ease of navigating in this code and around this logic (it can be in the same file, outside the render)

Copy link
Member Author

@Anemy Anemy Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into a generic hook in compass-components. Would it be better to keep it in data-modeling as there aren't any other consumers of the hook yet?

Errr I might need to update the useMemo usage that's outside of the hook. Probably nicer to have that encapsulated.

const lastDiagramUpdateMS = useRef(0);
const pendingUpdate = useRef<NodeJS.Timeout | null>(null);
const [throttledDiagramProps, setThrottledDiagramProps] = useState({
isDarkMode,
diagramLabel,
edges,
nodes,
onNodeClick,
onPaneClick,
onEdgeClick,
onFieldClick,
onNodeDragStop,
onConnect,
});

// Throttle diagram props updating. This ensures we don't run
// into broken state bugs with ReactFlow like COMPASS-9738.
useEffect(() => {
const now = Date.now();
const timeSinceLastUpdate = now - lastDiagramUpdateMS.current;

const updateProps = () => {
lastDiagramUpdateMS.current = Date.now();
setThrottledDiagramProps({
isDarkMode,
diagramLabel,
edges,
nodes,
onNodeClick,
onPaneClick,
onEdgeClick,
onFieldClick,
onNodeDragStop,
onConnect,
});
};

if (timeSinceLastUpdate >= REFRESH_DIAGRAM_RATE_MS) {
updateProps();
} else {
if (pendingUpdate.current) {
clearTimeout(pendingUpdate.current);
}

// Schedule update for the remaining time.
const remainingTime = REFRESH_DIAGRAM_RATE_MS - timeSinceLastUpdate;
pendingUpdate.current = setTimeout(updateProps, remainingTime);
}

return () => {
if (pendingUpdate.current) {
clearTimeout(pendingUpdate.current);
pendingUpdate.current = null;
}
};
}, [
isDarkMode,
diagramLabel,
edges,
nodes,
onNodeClick,
onPaneClick,
onEdgeClick,
onFieldClick,
onNodeDragStop,
onConnect,
]);

const diagramContent = useMemo(() => {
return (
<Diagram
isDarkMode={throttledDiagramProps.isDarkMode}
title={throttledDiagramProps.diagramLabel}
edges={throttledDiagramProps.edges}
nodes={throttledDiagramProps.nodes}
// With threshold too low clicking sometimes gets confused with
// dragging
nodeDragThreshold={5}
onNodeClick={throttledDiagramProps.onNodeClick}
onPaneClick={throttledDiagramProps.onPaneClick}
onEdgeClick={throttledDiagramProps.onEdgeClick}
onFieldClick={throttledDiagramProps.onFieldClick}
fitViewOptions={ZOOM_OPTIONS}
onNodeDragStop={throttledDiagramProps.onNodeDragStop}
onConnect={throttledDiagramProps.onConnect}
/>
);
}, [throttledDiagramProps]);

return (
<div
ref={setDiagramContainerRef}
Expand All @@ -340,22 +432,7 @@ const DiagramContent: React.FunctionComponent<{
impact your data
</Banner>
)}
<Diagram
isDarkMode={isDarkMode}
title={diagramLabel}
edges={edges}
nodes={nodes}
// With threshold too low clicking sometimes gets confused with
// dragging
nodeDragThreshold={5}
onNodeClick={onNodeClick}
onPaneClick={onPaneClick}
onEdgeClick={onEdgeClick}
onFieldClick={onFieldClick}
fitViewOptions={ZOOM_OPTIONS}
onNodeDragStop={onNodeDragStop}
onConnect={onConnect}
/>
{diagramContent}
</div>
</div>
);
Expand Down
Loading