Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions include/flow/core/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ class Graph
*/
EventDispatcher<const std::exception&> OnError;

/**
* @brief Event run on Graph when a new node is added.
*/
EventDispatcher<const SharedNode&> OnNodeAdded;

/**
* @brief Event run on Graph when a new node is removed.
*/
EventDispatcher<const SharedNode&> OnNodeRemoved;

/**
* @brief Event run when 2 nodes are connected.
*/
EventDispatcher<const SharedConnection&> OnNodesConnected;

/**
* @brief Event run on Graph when a connection is removed.
*/
EventDispatcher<const SharedConnection&> OnNodesDisconnected;

protected:
mutable std::mutex _nodes_mutex;

Expand Down
27 changes: 23 additions & 4 deletions src/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

UUID id = node->ID();
_nodes.emplace(id, std::move(node));
OnNodeAdded.Broadcast(node);
}

void Graph::RemoveNode(const SharedNode& node)
Expand All @@ -112,6 +113,9 @@ void Graph::RemoveNodeByID(const UUID& uuid)
if (found != _nodes.end())
{
found->second->Stop();

OnNodeRemoved.Broadcast(found->second);

_nodes.erase(found);
}
}
Expand Down Expand Up @@ -227,12 +231,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);
Expand Down
Loading