Skip to main content

jellyflow_runtime/runtime/utils/
connections.rs

1use crate::runtime::lookups::{ConnectionSide, NodeGraphLookups};
2use jellyflow_core::core::{EdgeId, NodeId};
3
4/// Returns the nodes connected as *targets* of the given node's outgoing edges.
5pub fn get_outgoers(lookups: &NodeGraphLookups, node: NodeId) -> Vec<NodeId> {
6    let Some(conns) = lookups.connections_for_node_side(node, ConnectionSide::Source) else {
7        return Vec::new();
8    };
9    sorted_unique(conns.values().map(|c| c.target_node).collect())
10}
11
12/// Returns the nodes connected as *sources* of the given node's incoming edges.
13pub fn get_incomers(lookups: &NodeGraphLookups, node: NodeId) -> Vec<NodeId> {
14    let Some(conns) = lookups.connections_for_node_side(node, ConnectionSide::Target) else {
15        return Vec::new();
16    };
17    sorted_unique(conns.values().map(|c| c.source_node).collect())
18}
19
20/// Returns all edges incident to the given node (both directions).
21pub fn get_connected_edges(lookups: &NodeGraphLookups, node: NodeId) -> Vec<EdgeId> {
22    let Some(conns) = lookups.connections_for_node(node) else {
23        return Vec::new();
24    };
25    sorted_unique(conns.values().map(|c| c.edge).collect())
26}
27
28/// Returns all edges connected to any node in the given set.
29///
30/// This matches the intent of XyFlow's `getConnectedEdges(nodes, edges)` helper, but uses
31/// `NodeGraphLookups` rather than scanning an edge array.
32pub fn get_connected_edges_for_nodes(
33    lookups: &NodeGraphLookups,
34    nodes: impl IntoIterator<Item = NodeId>,
35) -> Vec<EdgeId> {
36    let mut out: Vec<EdgeId> = Vec::new();
37    for node in nodes {
38        out.extend(get_connected_edges(lookups, node));
39    }
40    sorted_unique(out)
41}
42
43fn sorted_unique<T: Ord>(mut items: Vec<T>) -> Vec<T> {
44    items.sort();
45    items.dedup();
46    items
47}