fixed internal examples, made code more understandable

This commit is contained in:
peterkogo
2025-01-07 11:57:33 +01:00
parent 4a0b5e6c21
commit 4fc8e08128
9 changed files with 32 additions and 30 deletions
+4 -4
View File
@@ -49,7 +49,7 @@ import NodeToolbar from '../examples/NodeToolbar';
import UseConnection from '../examples/UseConnection'; import UseConnection from '../examples/UseConnection';
import UseNodesInitialized from '../examples/UseNodesInit'; import UseNodesInitialized from '../examples/UseNodesInit';
import UseNodesData from '../examples/UseNodesData'; import UseNodesData from '../examples/UseNodesData';
import UseHandleConnections from '../examples/UseHandleConnections'; import UseNodeConnections from '../examples/UseNodeConnections';
import AddNodeOnEdgeDrop from '../examples/AddNodeOnEdgeDrop'; import AddNodeOnEdgeDrop from '../examples/AddNodeOnEdgeDrop';
import DevTools from '../examples/DevTools'; import DevTools from '../examples/DevTools';
import Redux from '../examples/Redux'; import Redux from '../examples/Redux';
@@ -313,9 +313,9 @@ const routes: IRoute[] = [
component: UseReactFlow, component: UseReactFlow,
}, },
{ {
name: 'useHandleConnections', name: 'useNodeConnections',
path: 'usehandleconnections', path: 'usenodeconnections',
component: UseHandleConnections, component: UseNodeConnections,
}, },
{ {
name: 'useNodesData', name: 'useNodesData',
@@ -1,5 +1,5 @@
import { memo, FC, useEffect, useCallback } from 'react'; import { memo, FC, useEffect, useCallback } from 'react';
import { Handle, Position, NodeProps, useHandleConnections, Connection, HandleProps } from '@xyflow/react'; import { Handle, Position, NodeProps, useNodeConnections, Connection, HandleProps } from '@xyflow/react';
function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string }) { function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string }) {
const onConnect = useCallback( const onConnect = useCallback(
@@ -11,9 +11,9 @@ function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string
(connections: Connection[]) => console.log('onDisconnect handler, node id:', nodeId, connections), (connections: Connection[]) => console.log('onDisconnect handler, node id:', nodeId, connections),
[nodeId] [nodeId]
); );
const connections = useHandleConnections({ const connections = useNodeConnections({
type: handleProps.type, type: handleProps.type,
id: handleProps.id, handleId: handleProps.id,
onConnect, onConnect,
onDisconnect, onDisconnect,
}); });
@@ -1,5 +1,5 @@
import { memo, FC, useEffect, useCallback } from 'react'; import { memo, FC, useEffect, useCallback } from 'react';
import { Handle, Position, NodeProps, useHandleConnections, Connection, HandleProps } from '@xyflow/react'; import { Handle, Position, NodeProps, useNodeConnections, Connection, HandleProps } from '@xyflow/react';
function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string }) { function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string }) {
const onConnect = useCallback( const onConnect = useCallback(
@@ -14,9 +14,9 @@ function CustomHandle({ nodeId, ...handleProps }: HandleProps & { nodeId: string
}, },
[nodeId] [nodeId]
); );
const connections = useHandleConnections({ const connections = useNodeConnections({
type: handleProps.type, type: handleProps.type,
id: handleProps.id, handleId: handleProps.id,
onConnect, onConnect,
onDisconnect, onDisconnect,
}); });
@@ -1,9 +1,9 @@
import { memo } from 'react'; import { memo } from 'react';
import { Handle, Position, useHandleConnections, useNodesData } from '@xyflow/react'; import { Handle, Position, useNodeConnections, useNodesData } from '@xyflow/react';
import { isTextNode, type MyNode } from '.'; import { isTextNode, type MyNode } from '.';
function ResultNode() { function ResultNode() {
const connections = useHandleConnections({ const connections = useNodeConnections({
type: 'target', type: 'target',
}); });
const nodesData = useNodesData<MyNode>(connections.map((connection) => connection.source)); const nodesData = useNodesData<MyNode>(connections.map((connection) => connection.source));
@@ -1,10 +1,10 @@
import { memo, useEffect } from 'react'; import { memo, useEffect } from 'react';
import { Position, NodeProps, useReactFlow, Handle, useHandleConnections, useNodesData } from '@xyflow/react'; import { Position, NodeProps, useReactFlow, Handle, useNodeConnections, useNodesData } from '@xyflow/react';
import { isTextNode, type TextNode, type MyNode } from '.'; import { isTextNode, type TextNode, type MyNode } from '.';
function UppercaseNode({ id }: NodeProps) { function UppercaseNode({ id }: NodeProps) {
const { updateNodeData } = useReactFlow(); const { updateNodeData } = useReactFlow();
const connections = useHandleConnections({ const connections = useNodeConnections({
type: 'target', type: 'target',
}); });
const nodesData = useNodesData<MyNode>(connections[0]?.source); const nodesData = useNodesData<MyNode>(connections[0]?.source);
@@ -2,7 +2,7 @@
import { import {
Handle, Handle,
Position, Position,
useHandleConnections, useNodeConnections,
useNodesData, useNodesData,
type NodeProps type NodeProps
} from '@xyflow/svelte'; } from '@xyflow/svelte';
@@ -13,7 +13,7 @@
export let id: $$Props['id']; export let id: $$Props['id'];
$$restProps; $$restProps;
const connections = useHandleConnections({ const connections = useNodeConnections({
nodeId: id, nodeId: id,
type: 'target' type: 'target'
}); });
@@ -2,7 +2,7 @@
import { import {
Handle, Handle,
Position, Position,
useHandleConnections, useNodeConnections,
useNodesData, useNodesData,
useSvelteFlow, useSvelteFlow,
type NodeProps type NodeProps
@@ -16,7 +16,7 @@
$$restProps; $$restProps;
const { updateNodeData } = useSvelteFlow(); const { updateNodeData } = useSvelteFlow();
const connections = useHandleConnections({ const connections = useNodeConnections({
nodeId: id, nodeId: id,
type: 'target' type: 'target'
}); });
+14 -12
View File
@@ -457,18 +457,20 @@ function addConnectionToLookup(
nodeId: string, nodeId: string,
handleId: string | null handleId: string | null
) { ) {
// create array of key fragments for easier iteration // We add the connection to the connectionLookup at the following keys
const keyFragments = [nodeId, type, handleId]; // 1. nodeId, 2. nodeId-type, 3. nodeId-type-handleId
let key = ''; // If the key already exists, we add the connection to the existing map
for (const keyFragment of keyFragments) { let key = nodeId;
// values for each iteration: nodeId, nodeId-type, nodeId-type-handleId const nodeMap = connectionLookup.get(key) || new Map();
key += keyFragment; connectionLookup.set(key, nodeMap.set(connectionKey, connection));
// create a new map if the key does not exist and add the connection
const prevMap = connectionLookup.get(key) || new Map(); key = `${nodeId}-${type}`;
connectionLookup.set(key, prevMap.set(connectionKey, connection)); const typeMap = connectionLookup.get(key) || new Map();
// add - as seperator for next iteration connectionLookup.set(key, typeMap.set(connectionKey, connection));
key += '-';
} key = `${nodeId}-${type}-${handleId}`;
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[]) {