dont invoke connection updates if the snapped handle stays the same

This commit is contained in:
peterkogo
2024-06-26 18:12:56 +02:00
parent 41ebe46d77
commit 25dfeb1c02
2 changed files with 23 additions and 4 deletions

View File

@@ -314,6 +314,7 @@ const createStore = ({
});
},
updateConnection: (connection) => {
console.log('newUpdate');
set({ connection });
},

View File

@@ -14,6 +14,7 @@ import {
NodeLookup,
Position,
oppositePosition,
ConnectionInProgress,
} from '../types';
import { getClosestHandle, isConnectionValid, getHandleLookup, getHandleType } from './utils';
@@ -145,7 +146,7 @@ function onPointerDown(
const from = getHandlePosition(fromNodeInternal, fromHandle, Position.Left, true);
updateConnection({
const newConnection: ConnectionInProgress = {
inProgress: true,
isValid: null,
@@ -158,7 +159,10 @@ function onPointerDown(
toHandle: null,
toPosition: oppositePosition[fromHandle.position],
toNode: null,
});
};
updateConnection(newConnection);
let previousConnection: ConnectionInProgress = newConnection;
onConnectStart?.(event, { nodeId, handleId, handleType });
@@ -198,7 +202,7 @@ function onPointerDown(
connection = result.connection;
isValid = isConnectionValid(!!closestHandle, result.isValid);
updateConnection({
const newConnection: ConnectionInProgress = {
inProgress: true,
isValid,
@@ -214,7 +218,21 @@ function onPointerDown(
toHandle: result.toHandle,
toPosition: isValid && result.toHandle ? result.toHandle.position : oppositePosition[fromHandle.position],
toNode: result.toHandle ? nodeLookup.get(result.toHandle.nodeId)!.internals.userNode : null,
});
};
// we don't want to trigger an update when the connection
// is snapped to the same handle as before
if (
previousConnection.toHandle &&
newConnection.toHandle &&
previousConnection.toHandle.nodeId === newConnection.toHandle.nodeId &&
previousConnection.toHandle.id === newConnection.toHandle.id
) {
return;
}
updateConnection(newConnection);
previousConnection = newConnection;
}
function onPointerUp(event: MouseEvent | TouchEvent) {