File size: 1,196 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { derived } from 'svelte/store';
import { areConnectionMapsEqual } from '@xyflow/system';
import { useStore } from '../store';
import { getContext } from 'svelte';
const initialConnections = [];
/**
 *  Hook to check if a <Handle /> is connected to another <Handle /> and get the connections.
 *
 * @public
 * @param param.nodeId
 * @param param.type - handle type 'source' or 'target'
 * @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
 */
export function useHandleConnections({ type, nodeId, id = null }) {
    const { edges, connectionLookup } = useStore();
    const _nodeId = getContext('svelteflow__node_id');
    const currentNodeId = nodeId ?? _nodeId;
    let prevConnections = undefined;
    return derived([edges, connectionLookup], ([, connectionLookup], set) => {
        const nextConnections = connectionLookup.get(`${currentNodeId}-${type}-${id || null}`);
        if (!areConnectionMapsEqual(nextConnections, prevConnections)) {
            prevConnections = nextConnections;
            set(Array.from(prevConnections?.values() || []));
        }
    }, initialConnections);
}