1
1
import type { ComputedRef , MaybeRefOrGetter } from 'vue'
2
2
import { computed , ref , toRef , toValue , watch } from 'vue'
3
- import type { Connection , HandleType } from '../types'
3
+ import type { HandleConnection , HandleType } from '../types'
4
4
import { areConnectionMapsEqual , handleConnectionChange } from '../utils'
5
5
import { useNodeId } from './useNodeId'
6
6
import { useVueFlow } from './useVueFlow'
@@ -9,14 +9,12 @@ export interface UseHandleConnectionsParams {
9
9
type : MaybeRefOrGetter < HandleType >
10
10
id ?: MaybeRefOrGetter < string | null >
11
11
nodeId ?: MaybeRefOrGetter < string | null >
12
- onConnect ?: ( connections : Connection [ ] ) => void
13
- onDisconnect ?: ( connections : Connection [ ] ) => void
12
+ onConnect ?: ( connections : HandleConnection [ ] ) => void
13
+ onDisconnect ?: ( connections : HandleConnection [ ] ) => void
14
14
}
15
15
16
- // todo: add edge id to connection params
17
-
18
16
/**
19
- * Composable that returns existing connections of a handle
17
+ * Composable that returns existing connections of a `<Handle />`.
20
18
*
21
19
* @public
22
20
* @param params
@@ -28,7 +26,7 @@ export interface UseHandleConnectionsParams {
28
26
*
29
27
* @returns An array of connections
30
28
*/
31
- export function useHandleConnections ( params : UseHandleConnectionsParams ) : ComputedRef < Connection [ ] > {
29
+ export function useHandleConnections ( params : UseHandleConnectionsParams ) : ComputedRef < HandleConnection [ ] > {
32
30
const { type, id, nodeId, onConnect, onDisconnect } = params
33
31
34
32
const { connectionLookup } = useVueFlow ( )
@@ -41,9 +39,9 @@ export function useHandleConnections(params: UseHandleConnectionsParams): Comput
41
39
42
40
const handleId = toRef ( ( ) => toValue ( id ) ?? null )
43
41
44
- const prevConnections = ref < Map < string , Connection > | null > ( null )
42
+ const prevConnections = ref < Map < string , HandleConnection > | null > ( null )
45
43
46
- const connections = ref < Map < string , Connection > > ( )
44
+ const connections = ref < Map < string , HandleConnection > > ( )
47
45
48
46
watch (
49
47
( ) => connectionLookup . value . get ( `${ currentNodeId . value } -${ handleType . value } -${ handleId . value } ` ) ,
@@ -59,17 +57,22 @@ export function useHandleConnections(params: UseHandleConnectionsParams): Comput
59
57
60
58
watch (
61
59
[ connections , ( ) => typeof onConnect !== 'undefined' , ( ) => typeof onDisconnect !== 'undefined' ] ,
62
- ( [ currentConnections ] ) => {
60
+ ( [ currentConnections = new Map < string , HandleConnection > ( ) ] ) => {
63
61
if ( prevConnections . value && prevConnections . value !== currentConnections ) {
64
- const _connections = currentConnections ?? new Map ( )
65
- handleConnectionChange ( prevConnections . value , _connections , onDisconnect )
66
- handleConnectionChange ( _connections , prevConnections . value , onConnect )
62
+ handleConnectionChange ( prevConnections . value , currentConnections , onDisconnect )
63
+ handleConnectionChange ( currentConnections , prevConnections . value , onConnect )
67
64
}
68
65
69
- prevConnections . value = currentConnections ?? new Map ( )
66
+ prevConnections . value = currentConnections
70
67
} ,
71
68
{ immediate : true } ,
72
69
)
73
70
74
- return computed ( ( ) => Array . from ( connections . value ?. values ( ) ?? [ ] ) )
71
+ return computed ( ( ) => {
72
+ if ( ! connections . value ) {
73
+ return [ ]
74
+ }
75
+
76
+ return Array . from ( connections . value . values ( ) )
77
+ } )
75
78
}
0 commit comments