removePort method

void removePort(
  1. String nodeId,
  2. String portId
)

Removes a port from a node and all connections involving that port.

This method will:

  1. Remove all connections where this port is the source or target
  2. Remove the port from the node

Does nothing if the node with nodeId doesn't exist.

Implementation

void removePort(String nodeId, String portId) {
  final node = _nodes[nodeId];
  if (node == null) return;

  runInAction(() {
    // Find connections involving this port
    final connectionsToRemove = _connections
        .where(
          (c) =>
              (c.sourceNodeId == nodeId && c.sourcePortId == portId) ||
              (c.targetNodeId == nodeId && c.targetPortId == portId),
        )
        .toList();

    // Remove from spatial index and path cache
    for (final connection in connectionsToRemove) {
      _spatialIndex.removeConnection(connection.id);
      _connectionPainter?.removeConnectionFromCache(connection.id);
    }

    // Remove from connections list
    _connections.removeWhere(
      (c) =>
          (c.sourceNodeId == nodeId && c.sourcePortId == portId) ||
          (c.targetNodeId == nodeId && c.targetPortId == portId),
    );

    // Remove the port using the node's dynamic method
    node.removePort(portId);
  });
}