addConnection method

void addConnection(
  1. Connection<C> connection
)

Adds a connection between two ports.

Triggers the onConnectionCreated callback after successful addition.

Example:

final connection = Connection(
  id: 'conn1',
  sourceNodeId: 'node1',
  sourcePortId: 'out1',
  targetNodeId: 'node2',
  targetPortId: 'in1',
);
controller.addConnection(connection);

Implementation

void addConnection(Connection<C> connection) {
  runInAction(() {
    _connections.add(connection);
    // Update connection index for O(1) lookup
    _connectionsByNodeId
        .putIfAbsent(connection.sourceNodeId, () => {})
        .add(connection.id);
    _connectionsByNodeId
        .putIfAbsent(connection.targetNodeId, () => {})
        .add(connection.id);
  });
  // Fire event after successful addition
  events.connection?.onCreated?.call(connection);
  // Emit extension event
  _emitEvent(ConnectionAdded(connection));
}