Michael Mauderer 2023-07-05 17:28:20 +02:00 committed by GitHub
parent 78545b4402
commit cd7f17a0e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View File

@ -283,6 +283,16 @@ impl Connections {
target: Self::convert_endpoint(&connection.target),
}
}
/// Return all connections that involve the given node.
pub fn with_node(&self, node: node::Id) -> impl Iterator<Item = Connection> {
self.connections
.iter()
.filter(move |conn| conn.source.node == node || conn.target.node == node)
.copied()
.collect_vec()
.into_iter()
}
}

View File

@ -451,6 +451,21 @@ impl Handle {
}
}
/// Remove all the connections from the graph. This is a convenience method that calls
/// [`disconnect`] for each connection. If any of the calls fails, the first error is
/// propagated, but all the connections are attempted to be disconnected.
pub fn disconnect_all(&self, connections: impl Iterator<Item = Connection>) -> FallibleResult {
let errors =
connections.map(|c| self.disconnect(&c)).filter_map(|r| r.err()).collect::<Vec<_>>();
// Failure has no good way to propagate multiple errors with `Failure`. So we propagate
// only the first one.
if let Some(error) = errors.into_iter().next() {
Err(error)
} else {
Ok(())
}
}
/// Set the execution environment.
pub async fn set_execution_environment(
&self,

View File

@ -292,6 +292,17 @@ impl Model {
|| {
let ast_id = self.state.update_from_view().remove_node(id)?;
self.widget.remove_all_node_widgets(ast_id);
let connections = self.controller.connections();
let node_connections = connections.map(|c| c.with_node(ast_id));
let disconnect_result = node_connections.map(|c| self.controller.disconnect_all(c));
if let Err(e) = disconnect_result {
warn!(
"Failed to disconnect all connections from node {:?} because of {:?}",
ast_id, e
);
}
Some(self.controller.graph().remove_node(ast_id))
},
"remove node",