From 82154f5227f6f86a5eafb01a00814e553c40ab25 Mon Sep 17 00:00:00 2001 From: GhostofCookie Date: Thu, 9 Jan 2025 10:35:39 -0500 Subject: [PATCH 1/2] Adding more events to the graph to make it easier to tie UI events to the graph. --- include/flow/core/Graph.hpp | 20 ++++++++++++++++++++ src/Graph.cpp | 22 ++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/include/flow/core/Graph.hpp b/include/flow/core/Graph.hpp index 46648c2..4656998 100644 --- a/include/flow/core/Graph.hpp +++ b/include/flow/core/Graph.hpp @@ -184,6 +184,26 @@ class Graph */ EventDispatcher OnError; + /** + * @brief Event run on Graph when a new node is added. + */ + EventDispatcher OnNodeAdded; + + /** + * @brief Event run on Graph when a new node is removed. + */ + EventDispatcher OnNodeRemoved; + + /** + * @brief Event run when 2 nodes are connected. + */ + EventDispatcher OnNodesConnected; + + /** + * @brief Event run on Graph when a connection is removed. + */ + EventDispatcher OnNodesDisconnected; + protected: mutable std::mutex _nodes_mutex; diff --git a/src/Graph.cpp b/src/Graph.cpp index cf60fcc..d319632 100644 --- a/src/Graph.cpp +++ b/src/Graph.cpp @@ -91,8 +91,8 @@ void Graph::AddNode(SharedNode node) this->PropagateConnectionsData(id, key, std::move(data)); }); - UUID id = node->ID(); - _nodes.emplace(id, std::move(node)); + OnNodeAdded.Broadcast(node); + _nodes.emplace(node->ID(), std::move(node)); } void Graph::RemoveNode(const SharedNode& node) @@ -112,6 +112,9 @@ void Graph::RemoveNodeByID(const UUID& uuid) if (found != _nodes.end()) { found->second->Stop(); + + OnNodeRemoved.Broadcast(found->second); + _nodes.erase(found); } } @@ -227,12 +230,27 @@ SharedConnection Graph::ConnectNodes(const UUID& start_id, const IndexableName& PropagateConnectionsData(start_id, start_port_key, std::move(data)); } + OnNodesConnected.Broadcast(conn); + return conn; } void Graph::DisconnectNodes(const UUID& start_id, const IndexableName& start_port_key, const UUID& end_id, const IndexableName& end_port_key) { + + auto conns = _connections.FindConnections(start_id, start_port_key); + auto found_conn = std::find_if(conns.begin(), conns.end(), [&](const auto& conn) { + return conn->EndNodeID() == end_id && conn->EndPortKey() == end_port_key; + }); + + if (found_conn == conns.end()) + { + return; + } + + OnNodesDisconnected.Broadcast(*found_conn); + _connections.Remove(start_id, end_id); auto in_node = GetNode(start_id); From 4a72fae684dac5fbc39be3c278fbbb3d4284ee65 Mon Sep 17 00:00:00 2001 From: GhostofCookie Date: Thu, 9 Jan 2025 14:13:19 -0500 Subject: [PATCH 2/2] Fix deadlock issue. --- src/Graph.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Graph.cpp b/src/Graph.cpp index d319632..f2cf3bf 100644 --- a/src/Graph.cpp +++ b/src/Graph.cpp @@ -84,15 +84,16 @@ void Graph::AddNode(SharedNode node) { if (!node) return; - std::lock_guard _(_nodes_mutex); + { + std::lock_guard _(_nodes_mutex); + _nodes.emplace(node->ID(), node); + } node->OnEmitOutput.Bind("PropagateConnectionsData", [this](const UUID& id, const IndexableName& key, SharedNodeData data) { this->PropagateConnectionsData(id, key, std::move(data)); }); - OnNodeAdded.Broadcast(node); - _nodes.emplace(node->ID(), std::move(node)); } void Graph::RemoveNode(const SharedNode& node)