Skip to content

Commit 9791448

Browse files
committed
chore(core): cleanup types
Signed-off-by: braks <[email protected]>
1 parent 54ea8a0 commit 9791448

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/core/src/utils/graph.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
EdgeMarkerType,
1111
ElementData,
1212
Elements,
13+
FlowElements,
1314
GraphEdge,
1415
GraphNode,
1516
MaybeElement,
@@ -127,15 +128,21 @@ export function parseEdge(edge: Edge, defaults: Partial<GraphEdge> = {}): GraphE
127128
return Object.assign({ id: edge.id.toString() }, edge, defaults) as GraphEdge
128129
}
129130

130-
const getConnectedElements = (node: GraphNode, elements: Elements, dir: 'source' | 'target') => {
131+
const getConnectedElements = <T extends Elements = FlowElements>(
132+
node: Node,
133+
elements: T,
134+
dir: 'source' | 'target',
135+
): T extends FlowElements ? GraphNode[] : Node[] => {
131136
if (!isNode(node)) return []
132137
const origin = dir === 'source' ? 'target' : 'source'
133138
const ids = elements.filter((e) => isEdge(e) && e[origin] === node.id).map((e) => isEdge(e) && e[dir])
134-
return elements.filter((e) => ids.includes(e.id)) as GraphEdge[]
139+
return elements.filter((e) => ids.includes(e.id)) as T extends FlowElements ? GraphNode[] : Node[]
135140
}
136-
export const getOutgoers = (node: GraphNode, elements: Elements) => getConnectedElements(node, elements, 'target')
141+
export const getOutgoers = <T extends Elements = FlowElements>(node: Node, elements: T) =>
142+
getConnectedElements(node, elements, 'target')
137143

138-
export const getIncomers = (node: GraphNode, elements: Elements) => getConnectedElements(node, elements, 'source')
144+
export const getIncomers = <T extends Elements = FlowElements>(node: Node, elements: T) =>
145+
getConnectedElements(node, elements, 'source')
139146

140147
export const getEdgeId = ({ source, sourceHandle, target, targetHandle }: Connection) =>
141148
`vueflow__edge-${source}${sourceHandle ?? ''}-${target}${targetHandle ?? ''}`

0 commit comments

Comments
 (0)