Skip to content

Commit 1bb7a19

Browse files
committed
feat(core): add edge id to handle connections
1 parent fa7087f commit 1bb7a19

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

packages/core/src/composables/useHandleConnections.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ComputedRef, MaybeRefOrGetter } from 'vue'
22
import { computed, ref, toRef, toValue, watch } from 'vue'
3-
import type { Connection, HandleType } from '../types'
3+
import type { HandleConnection, HandleType } from '../types'
44
import { areConnectionMapsEqual, handleConnectionChange } from '../utils'
55
import { useNodeId } from './useNodeId'
66
import { useVueFlow } from './useVueFlow'
@@ -9,14 +9,12 @@ export interface UseHandleConnectionsParams {
99
type: MaybeRefOrGetter<HandleType>
1010
id?: MaybeRefOrGetter<string | null>
1111
nodeId?: MaybeRefOrGetter<string | null>
12-
onConnect?: (connections: Connection[]) => void
13-
onDisconnect?: (connections: Connection[]) => void
12+
onConnect?: (connections: HandleConnection[]) => void
13+
onDisconnect?: (connections: HandleConnection[]) => void
1414
}
1515

16-
// todo: add edge id to connection params
17-
1816
/**
19-
* Composable that returns existing connections of a handle
17+
* Composable that returns existing connections of a `<Handle />`.
2018
*
2119
* @public
2220
* @param params
@@ -28,7 +26,7 @@ export interface UseHandleConnectionsParams {
2826
*
2927
* @returns An array of connections
3028
*/
31-
export function useHandleConnections(params: UseHandleConnectionsParams): ComputedRef<Connection[]> {
29+
export function useHandleConnections(params: UseHandleConnectionsParams): ComputedRef<HandleConnection[]> {
3230
const { type, id, nodeId, onConnect, onDisconnect } = params
3331

3432
const { connectionLookup } = useVueFlow()
@@ -41,9 +39,9 @@ export function useHandleConnections(params: UseHandleConnectionsParams): Comput
4139

4240
const handleId = toRef(() => toValue(id) ?? null)
4341

44-
const prevConnections = ref<Map<string, Connection> | null>(null)
42+
const prevConnections = ref<Map<string, HandleConnection> | null>(null)
4543

46-
const connections = ref<Map<string, Connection>>()
44+
const connections = ref<Map<string, HandleConnection>>()
4745

4846
watch(
4947
() => connectionLookup.value.get(`${currentNodeId.value}-${handleType.value}-${handleId.value}`),
@@ -59,17 +57,22 @@ export function useHandleConnections(params: UseHandleConnectionsParams): Comput
5957

6058
watch(
6159
[connections, () => typeof onConnect !== 'undefined', () => typeof onDisconnect !== 'undefined'],
62-
([currentConnections]) => {
60+
([currentConnections = new Map<string, HandleConnection>()]) => {
6361
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)
6764
}
6865

69-
prevConnections.value = currentConnections ?? new Map()
66+
prevConnections.value = currentConnections
7067
},
7168
{ immediate: true },
7269
)
7370

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+
})
7578
}

packages/core/src/types/connection.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export interface Connection {
3333
targetHandle?: string | null
3434
}
3535

36+
/** Connection with edge id */
37+
export interface HandleConnection extends Connection {
38+
edgeId: string
39+
}
40+
3641
export type Connector = (
3742
params: Connection,
3843
) => Promise<(Connection & Partial<Edge>) | false> | ((Connection & Partial<Edge>) | false)
@@ -83,3 +88,5 @@ export interface ConnectionLineProps {
8388
/** status of the connection (valid, invalid) */
8489
connectionStatus: ConnectionStatus | null
8590
}
91+
92+
export type ConnectionLookup = Map<string, Map<string, HandleConnection>>

packages/core/src/types/store.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
Connection,
2020
ConnectionLineOptions,
2121
ConnectionLineType,
22+
ConnectionLookup,
2223
ConnectionMode,
2324
ConnectionStatus,
2425
Connector,
@@ -30,8 +31,6 @@ import type { CustomEvent, FlowHooks, FlowHooksEmit, FlowHooksOn } from './hooks
3031
import type { EdgeChange, NodeChange, NodeDragItem } from './changes'
3132
import type { ConnectingHandle, ValidConnectionFunc } from './handle'
3233

33-
export type ConnectionLookup = Map<string, Map<string, Connection>>
34-
3534
export interface UpdateNodeDimensionsParams {
3635
id: string
3736
nodeElement: HTMLDivElement

packages/core/src/utils/store.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
Edge,
88
GraphEdge,
99
GraphNode,
10+
HandleConnection,
1011
Node,
1112
State,
1213
ValidConnectionFunc,
@@ -162,15 +163,15 @@ export function updateConnectionLookup(connectionLookup: ConnectionLookup, edges
162163
* @internal
163164
*/
164165
export function handleConnectionChange(
165-
a: Map<string, Connection>,
166-
b: Map<string, Connection>,
167-
cb?: (diff: Connection[]) => void,
166+
a: Map<string, HandleConnection>,
167+
b: Map<string, HandleConnection>,
168+
cb?: (diff: HandleConnection[]) => void,
168169
) {
169170
if (!cb) {
170171
return
171172
}
172173

173-
const diff: Connection[] = []
174+
const diff: HandleConnection[] = []
174175

175176
for (const key of a.keys()) {
176177
if (!b.has(key)) {

0 commit comments

Comments
 (0)