remove redundant connectionLookup entries

This commit is contained in:
peterkogo
2025-01-07 14:18:05 +01:00
parent b601d385f3
commit b2d1844c0f
6 changed files with 37 additions and 11 deletions
@@ -32,7 +32,7 @@ type useHandleConnectionsParams = {
*/ */
export function useHandleConnections({ export function useHandleConnections({
type, type,
id = null, id,
nodeId, nodeId,
onConnect, onConnect,
onDisconnect, onDisconnect,
@@ -47,7 +47,7 @@ export function useHandleConnections({
const prevConnections = useRef<Map<string, HandleConnection> | null>(null); const prevConnections = useRef<Map<string, HandleConnection> | null>(null);
const connections = useStore( const connections = useStore(
(state) => state.connectionLookup.get(`${currentNodeId}-${type}-${id}`), (state) => state.connectionLookup.get(`${currentNodeId}-${type}${id ? `-${id}` : ''}`),
areConnectionMapsEqual areConnectionMapsEqual
); );
+8 -1
View File
@@ -239,7 +239,14 @@ export function useReactFlow<NodeType extends Node = Node, EdgeType extends Edge
Array.from( Array.from(
store store
.getState() .getState()
.connectionLookup.get(`${nodeId}-${type}-${id ?? null}`) .connectionLookup.get(`${nodeId}-${type}${id ? `-${id}` : ''}`)
?.values() ?? []
),
getNodeConnections: ({ type, handleId, nodeId }) =>
Array.from(
store
.getState()
.connectionLookup.get(`${nodeId}${type ? (handleId ? `-${type}-${handleId}` : `-${type}`) : ''}`)
?.values() ?? [] ?.values() ?? []
), ),
}; };
+19 -2
View File
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-namespace */ /* eslint-disable @typescript-eslint/no-namespace */
import type { HandleConnection, HandleType, Rect, Viewport } from '@xyflow/system'; import type { HandleConnection, HandleType, NodeConnection, Rect, Viewport } from '@xyflow/system';
import type { Node, Edge, ViewportHelperFunctions, InternalNode } from '.'; import type { Node, Edge, ViewportHelperFunctions, InternalNode } from '.';
export type ReactFlowJsonObject<NodeType extends Node = Node, EdgeType extends Edge = Edge> = { export type ReactFlowJsonObject<NodeType extends Node = Node, EdgeType extends Edge = Edge> = {
@@ -183,7 +183,7 @@ export type GeneralHelpers<NodeType extends Node = Node, EdgeType extends Edge =
getNodesBounds: (nodes: (NodeType | InternalNode | string)[]) => Rect; getNodesBounds: (nodes: (NodeType | InternalNode | string)[]) => Rect;
/** /**
* Gets all connections for a given handle belonging to a specific node. * Gets all connections for a given handle belonging to a specific node.
* * @de
* @param type - handle type 'source' or 'target' * @param type - handle type 'source' or 'target'
* @param id - the handle id (this is only needed if you have multiple handles of the same type, meaning you have to provide a unique id for each handle) * @param id - the handle id (this is only needed if you have multiple handles of the same type, meaning you have to provide a unique id for each handle)
* @param nodeId - the node id the handle belongs to * @param nodeId - the node id the handle belongs to
@@ -198,6 +198,23 @@ export type GeneralHelpers<NodeType extends Node = Node, EdgeType extends Edge =
nodeId: string; nodeId: string;
id?: string | null; id?: string | null;
}) => HandleConnection[]; }) => HandleConnection[];
/**
* Gets all connections to a node. Can be filtered by handle type and id.
* @deprecated use `getNodeConnections` instead
* @param type - handle type 'source' or 'target'
* @param handleId - the handle id (this is only needed if you have multiple handles of the same type, meaning you have to provide a unique id for each handle)
* @param nodeId - the node id the handle belongs to
* @returns an array with handle connections
*/
getNodeConnections: ({
type,
handleId,
nodeId,
}: {
type?: HandleType;
nodeId: string;
handleId?: string | null;
}) => NodeConnection[];
}; };
export type ReactFlowInstance<NodeType extends Node = Node, EdgeType extends Edge = Edge> = GeneralHelpers< export type ReactFlowInstance<NodeType extends Node = Node, EdgeType extends Edge = Edge> = GeneralHelpers<
@@ -116,7 +116,7 @@
$: if (onconnect || ondisconnect) { $: if (onconnect || ondisconnect) {
// connectionLookup is not reactive, so we use edges to get notified about updates // connectionLookup is not reactive, so we use edges to get notified about updates
$edges; $edges;
connections = $connectionLookup.get(`${nodeId}-${type}-${id || null}`); connections = $connectionLookup.get(`${nodeId}-${type}${id ? `-${id}` : ''}`);
} }
$: { $: {
@@ -22,7 +22,7 @@ const initialConnections: HandleConnection[] = [];
* @param param.id - the handle id (this is only needed if the node has multiple handles of the same type) * @param param.id - the handle id (this is only needed if the node has multiple handles of the same type)
* @returns an array with connections * @returns an array with connections
*/ */
export function useHandleConnections({ type, nodeId, id = null }: useHandleConnectionsParams) { export function useHandleConnections({ type, nodeId, id }: useHandleConnectionsParams) {
const { edges, connectionLookup } = useStore(); const { edges, connectionLookup } = useStore();
console.warn( console.warn(
@@ -37,7 +37,7 @@ export function useHandleConnections({ type, nodeId, id = null }: useHandleConne
return derived( return derived(
[edges, connectionLookup], [edges, connectionLookup],
([, connectionLookup], set) => { ([, connectionLookup], set) => {
const nextConnections = connectionLookup.get(`${currentNodeId}-${type}-${id || null}`); const nextConnections = connectionLookup.get(`${currentNodeId}-${type}${id ? `-${id}` : ''}`);
if (!areConnectionMapsEqual(nextConnections, prevConnections)) { if (!areConnectionMapsEqual(nextConnections, prevConnections)) {
prevConnections = nextConnections; prevConnections = nextConnections;
+5 -3
View File
@@ -468,9 +468,11 @@ function addConnectionToLookup(
const typeMap = connectionLookup.get(key) || new Map(); const typeMap = connectionLookup.get(key) || new Map();
connectionLookup.set(key, typeMap.set(connectionKey, connection)); connectionLookup.set(key, typeMap.set(connectionKey, connection));
key = `${nodeId}-${type}-${handleId}`; if (handleId) {
const handleMap = connectionLookup.get(key) || new Map(); key = `${nodeId}-${type}-${handleId}`;
connectionLookup.set(key, handleMap.set(connectionKey, connection)); const handleMap = connectionLookup.get(key) || new Map();
connectionLookup.set(key, handleMap.set(connectionKey, connection));
}
} }
export function updateConnectionLookup(connectionLookup: ConnectionLookup, edgeLookup: EdgeLookup, edges: EdgeBase[]) { export function updateConnectionLookup(connectionLookup: ConnectionLookup, edgeLookup: EdgeLookup, edges: EdgeBase[]) {