Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 46 additions & 0 deletions packages/compass-components/src/hooks/use-throttled-props.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useEffect, useRef, useState } from 'react';

const DEFAULT_REFRESH_RATE_MS = 250;

export const useThrottledProps = <T extends Record<string, unknown>>(
props: T,
refreshRate: number = DEFAULT_REFRESH_RATE_MS
): T => {
// Throttling mechanism for Component content updates.
const lastUpdateMS = useRef(0);
const pendingUpdate = useRef<NodeJS.Timeout | null>(null);
const [throttledProps, setThrottledProps] = useState<T>(props);

// Throttle 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 - lastUpdateMS.current;

const updateProps = () => {
lastUpdateMS.current = Date.now();
setThrottledProps(props);
};

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

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

return () => {
if (pendingUpdate.current) {
clearTimeout(pendingUpdate.current);
pendingUpdate.current = null;
}
};
}, [props, refreshRate]);

return throttledProps;
};
1 change: 1 addition & 0 deletions packages/compass-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export {
Theme,
ThemeProvider,
} from './hooks/use-theme';
export { useThrottledProps } from './hooks/use-throttled-props';
export {
ContentWithFallback,
FadeInPlaceholder,
Expand Down
46 changes: 34 additions & 12 deletions packages/compass-data-modeling/src/components/diagram-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
useDarkMode,
useDrawerActions,
useDrawerState,
useThrottledProps,
rafraf,
} from '@mongodb-js/compass-components';
import { cancelAnalysis, retryAnalysis } from '../store/analysis-process';
Expand Down Expand Up @@ -324,6 +325,37 @@ const DiagramContent: React.FunctionComponent<{
[onAddNewFieldToCollection]
);

const diagramProps = useMemo(
() => ({
isDarkMode,
title: diagramLabel,
edges,
nodes,
onAddFieldToNodeClick: onClickAddFieldToCollection,
onNodeClick,
onPaneClick,
onEdgeClick,
onFieldClick,
onNodeDragStop,
onConnect,
}),
[
isDarkMode,
diagramLabel,
edges,
nodes,
onClickAddFieldToCollection,
onNodeClick,
onPaneClick,
onEdgeClick,
onFieldClick,
onNodeDragStop,
onConnect,
]
);

const throttledDiagramProps = useThrottledProps(diagramProps);

return (
<div
ref={setDiagramContainerRef}
Expand All @@ -346,21 +378,11 @@ const DiagramContent: React.FunctionComponent<{
</Banner>
)}
<Diagram
isDarkMode={isDarkMode}
title={diagramLabel}
edges={edges}
nodes={nodes}
{...throttledDiagramProps}
// With threshold too low clicking sometimes gets confused with
// dragging
// dragging.
nodeDragThreshold={5}
onNodeClick={onNodeClick}
onPaneClick={onPaneClick}
onEdgeClick={onEdgeClick}
onFieldClick={onFieldClick}
fitViewOptions={ZOOM_OPTIONS}
onNodeDragStop={onNodeDragStop}
onConnect={onConnect}
onAddFieldToNodeClick={onClickAddFieldToCollection}
/>
</div>
</div>
Expand Down
Loading