Skip to content

Commit 2a597f8

Browse files
committed
feat(core): allow passing node or id to getoutgoers/getincomers
1 parent c4acb0d commit 2a597f8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

packages/core/src/utils/graph.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,23 @@ export function parseEdge(edge: Edge, defaults: Partial<GraphEdge> = {}): GraphE
152152
}
153153

154154
function getConnectedElements<T extends Elements = FlowElements>(
155-
node: Node,
155+
nodeOrId: Node | { id: string } | string,
156156
elements: T,
157157
dir: 'source' | 'target',
158158
): T extends FlowElements ? GraphNode[] : Node[] {
159-
if (!isNode(node)) {
160-
return []
161-
}
159+
const id = isString(nodeOrId) ? nodeOrId : nodeOrId.id
160+
162161
const origin = dir === 'source' ? 'target' : 'source'
163-
const ids = elements.filter((e) => isEdge(e) && e[origin] === node.id).map((e) => isEdge(e) && e[dir])
162+
const ids = elements.filter((e) => isEdge(e) && e[origin] === id).map((e) => isEdge(e) && e[dir])
163+
164164
return elements.filter((e) => ids.includes(e.id)) as T extends FlowElements ? GraphNode[] : Node[]
165165
}
166-
export function getOutgoers<T extends Elements = FlowElements>(node: Node, elements: T) {
167-
return getConnectedElements(node, elements, 'target')
166+
export function getOutgoers<T extends Elements = FlowElements>(nodeOrId: Node | { id: string } | string, elements: T) {
167+
return getConnectedElements(nodeOrId, elements, 'target')
168168
}
169169

170-
export function getIncomers<T extends Elements = FlowElements>(node: Node, elements: T) {
171-
return getConnectedElements(node, elements, 'source')
170+
export function getIncomers<T extends Elements = FlowElements>(nodeOrId: Node | { id: string } | string, elements: T) {
171+
return getConnectedElements(nodeOrId, elements, 'source')
172172
}
173173

174174
export function getEdgeId({ source, sourceHandle, target, targetHandle }: Connection) {

0 commit comments

Comments
 (0)