Merge pull request #3914 from xyflow/handle-connections-svelte
aligned useHandleConnections in svelte with react version
This commit is contained in:
@@ -37,8 +37,9 @@ export function useHandleConnections({
|
|||||||
onDisconnect,
|
onDisconnect,
|
||||||
}: useHandleConnectionsParams): HandleConnection[] {
|
}: useHandleConnectionsParams): HandleConnection[] {
|
||||||
const _nodeId = useNodeId();
|
const _nodeId = useNodeId();
|
||||||
|
const currentNodeId = nodeId ?? _nodeId;
|
||||||
|
|
||||||
const prevConnections = useRef<Map<string, HandleConnection> | null>(null);
|
const prevConnections = useRef<Map<string, HandleConnection> | null>(null);
|
||||||
const currentNodeId = nodeId || _nodeId;
|
|
||||||
|
|
||||||
const connections = useStore(
|
const connections = useStore(
|
||||||
(state) => state.connectionLookup.get(`${currentNodeId}-${type}-${id}`),
|
(state) => state.connectionLookup.get(`${currentNodeId}-${type}-${id}`),
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ import { derived } from 'svelte/store';
|
|||||||
import { areConnectionMapsEqual, type HandleConnection, type HandleType } from '@xyflow/system';
|
import { areConnectionMapsEqual, type HandleConnection, type HandleType } from '@xyflow/system';
|
||||||
|
|
||||||
import { useStore } from '$lib/store';
|
import { useStore } from '$lib/store';
|
||||||
|
import { getContext } from 'svelte';
|
||||||
|
|
||||||
export type useHandleConnectionsParams = {
|
export type useHandleConnectionsParams = {
|
||||||
nodeId: string;
|
|
||||||
type: HandleType;
|
type: HandleType;
|
||||||
|
nodeId?: string;
|
||||||
id?: string | null;
|
id?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -20,14 +21,18 @@ 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({ nodeId, type, id = null }: useHandleConnectionsParams) {
|
export function useHandleConnections({ type, nodeId, id = null }: useHandleConnectionsParams) {
|
||||||
const { edges, connectionLookup } = useStore();
|
const { edges, connectionLookup } = useStore();
|
||||||
|
|
||||||
|
const _nodeId = getContext<string>('svelteflow__node_id');
|
||||||
|
const currentNodeId = nodeId ?? _nodeId;
|
||||||
|
|
||||||
let prevConnections: Map<string, HandleConnection> | undefined = undefined;
|
let prevConnections: Map<string, HandleConnection> | undefined = undefined;
|
||||||
|
|
||||||
return derived(
|
return derived(
|
||||||
[edges, connectionLookup],
|
[edges, connectionLookup],
|
||||||
([, connectionLookup], set) => {
|
([, connectionLookup], set) => {
|
||||||
const nextConnections = connectionLookup.get(`${nodeId}-${type}-${id || null}`);
|
const nextConnections = connectionLookup.get(`${currentNodeId}-${type}-${id || null}`);
|
||||||
|
|
||||||
if (!areConnectionMapsEqual(nextConnections, prevConnections)) {
|
if (!areConnectionMapsEqual(nextConnections, prevConnections)) {
|
||||||
prevConnections = nextConnections;
|
prevConnections = nextConnections;
|
||||||
|
|||||||
Reference in New Issue
Block a user