refactor(useHandleConnections): also return edge id

This commit is contained in:
moklick
2024-02-08 13:32:37 +01:00
parent 57270f3eb4
commit c3d5c8cdfa
6 changed files with 28 additions and 18 deletions
@@ -1,5 +1,11 @@
import { useEffect, useMemo, useRef } from 'react'; import { useEffect, useMemo, useRef } from 'react';
import { Connection, HandleType, areConnectionMapsEqual, handleConnectionChange } from '@xyflow/system'; import {
Connection,
HandleConnection,
HandleType,
areConnectionMapsEqual,
handleConnectionChange,
} from '@xyflow/system';
import { useStore } from './useStore'; import { useStore } from './useStore';
import { useNodeId } from '../contexts/NodeIdContext'; import { useNodeId } from '../contexts/NodeIdContext';
@@ -21,7 +27,7 @@ type useHandleConnectionsParams = {
* @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)
* @param param.onConnect - gets called when a connection is established * @param param.onConnect - gets called when a connection is established
* @param param.onDisconnect - gets called when a connection is removed * @param param.onDisconnect - gets called when a connection is removed
* @returns an array with connections * @returns an array with handle connections
*/ */
export function useHandleConnections({ export function useHandleConnections({
type, type,
@@ -29,9 +35,9 @@ export function useHandleConnections({
nodeId, nodeId,
onConnect, onConnect,
onDisconnect, onDisconnect,
}: useHandleConnectionsParams): Connection[] { }: useHandleConnectionsParams): HandleConnection[] {
const _nodeId = useNodeId(); const _nodeId = useNodeId();
const prevConnections = useRef<Map<string, Connection> | null>(null); const prevConnections = useRef<Map<string, HandleConnection> | null>(null);
const currentNodeId = nodeId || _nodeId; const currentNodeId = nodeId || _nodeId;
const connections = useStore( const connections = useStore(
@@ -6,7 +6,7 @@
Position, Position,
XYHandle, XYHandle,
isMouseEvent, isMouseEvent,
type Connection, type HandleConnection,
areConnectionMapsEqual, areConnectionMapsEqual,
handleConnectionChange handleConnectionChange
} from '@xyflow/system'; } from '@xyflow/system';
@@ -103,8 +103,8 @@
} }
} }
let prevConnections: Map<string, Connection> | null = null; let prevConnections: Map<string, HandleConnection> | null = null;
let connections: Map<string, Connection> | undefined; let connections: Map<string, HandleConnection> | undefined;
$: 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
@@ -1,5 +1,5 @@
import { derived } from 'svelte/store'; import { derived } from 'svelte/store';
import { areConnectionMapsEqual, type Connection, type HandleType } from '@xyflow/system'; import { areConnectionMapsEqual, type HandleConnection, type HandleType } from '@xyflow/system';
import { useStore } from '$lib/store'; import { useStore } from '$lib/store';
@@ -9,7 +9,7 @@ export type useHandleConnectionsParams = {
id?: string | null; id?: string | null;
}; };
const initialConnections: Connection[] = []; const initialConnections: HandleConnection[] = [];
/** /**
* Hook to check if a <Handle /> is connected to another <Handle /> and get the connections. * Hook to check if a <Handle /> is connected to another <Handle /> and get the connections.
@@ -22,7 +22,7 @@ const initialConnections: Connection[] = [];
*/ */
export function useHandleConnections({ nodeId, type, id = null }: useHandleConnectionsParams) { export function useHandleConnections({ nodeId, type, id = null }: useHandleConnectionsParams) {
const { edges, connectionLookup } = useStore(); const { edges, connectionLookup } = useStore();
let prevConnections: Map<string, Connection> | undefined = undefined; let prevConnections: Map<string, HandleConnection> | undefined = undefined;
return derived( return derived(
[edges, connectionLookup], [edges, connectionLookup],
+5 -1
View File
@@ -28,6 +28,10 @@ export type Connection = {
targetHandle: string | null; targetHandle: string | null;
}; };
export type HandleConnection = Connection & {
edgeId: string;
};
export type ConnectionStatus = 'valid' | 'invalid'; export type ConnectionStatus = 'valid' | 'invalid';
export enum ConnectionMode { export enum ConnectionMode {
@@ -136,7 +140,7 @@ export type UpdateConnection = (params: {
export type ColorModeClass = 'light' | 'dark'; export type ColorModeClass = 'light' | 'dark';
export type ColorMode = ColorModeClass | 'system'; export type ColorMode = ColorModeClass | 'system';
export type ConnectionLookup = Map<string, Map<string, Connection>>; export type ConnectionLookup = Map<string, Map<string, HandleConnection>>;
export type OnBeforeDeleteBase<NodeType extends NodeBase = NodeBase, EdgeType extends EdgeBase = EdgeBase> = ({ export type OnBeforeDeleteBase<NodeType extends NodeBase = NodeBase, EdgeType extends EdgeBase = EdgeBase> = ({
nodes, nodes,
+6 -6
View File
@@ -1,9 +1,9 @@
import { Connection } from '../types'; import { HandleConnection } from '../types';
/** /**
* @internal * @internal
*/ */
export function areConnectionMapsEqual(a?: Map<string, Connection>, b?: Map<string, Connection>) { export function areConnectionMapsEqual(a?: Map<string, HandleConnection>, b?: Map<string, HandleConnection>) {
if (!a && !b) { if (!a && !b) {
return true; return true;
} }
@@ -31,15 +31,15 @@ export function areConnectionMapsEqual(a?: Map<string, Connection>, b?: Map<stri
* @internal * @internal
*/ */
export function handleConnectionChange( export function handleConnectionChange(
a: Map<string, Connection>, a: Map<string, HandleConnection>,
b: Map<string, Connection>, b: Map<string, HandleConnection>,
cb?: (diff: Connection[]) => void cb?: (diff: HandleConnection[]) => void
) { ) {
if (!cb) { if (!cb) {
return; return;
} }
const diff: Connection[] = []; const diff: HandleConnection[] = [];
a.forEach((connection, key) => { a.forEach((connection, key) => {
if (!b?.has(key)) { if (!b?.has(key)) {
+1 -1
View File
@@ -260,7 +260,7 @@ export function updateConnectionLookup(connectionLookup: ConnectionLookup, edgeL
const prevSource = connectionLookup.get(sourceKey) || new Map(); const prevSource = connectionLookup.get(sourceKey) || new Map();
const prevTarget = connectionLookup.get(targetKey) || new Map(); const prevTarget = connectionLookup.get(targetKey) || new Map();
const connection = { source, target, sourceHandle, targetHandle }; const connection = { edgeId: edge.id, source, target, sourceHandle, targetHandle };
edgeLookup.set(edge.id, edge); edgeLookup.set(edge.id, edge);
connectionLookup.set(sourceKey, prevSource.set(`${target}-${targetHandle}`, connection)); connectionLookup.set(sourceKey, prevSource.set(`${target}-${targetHandle}`, connection));