removeConnection method

void removeConnection(
  1. String connectionId
)

Removes a connection from the graph.

This is a direct removal method that does NOT check:

  • Lock status
  • Before-delete callbacks

For deletion with lock and callback checks, use requestDeleteConnection.

Also removes the connection from the selection set if it was selected.

Triggers the onConnectionDeleted callback after successful removal.

Throws ArgumentError if the connection doesn't exist.

Implementation

void removeConnection(String connectionId) {
  final connectionToDelete = _connections.firstWhere(
    (c) => c.id == connectionId,
    orElse: () => throw ArgumentError('Connection $connectionId not found'),
  );
  runInAction(() {
    _connections.removeWhere((c) => c.id == connectionId);
    _selectedConnectionIds.remove(connectionId);

    // Update connection index for O(1) lookup
    _connectionsByNodeId[connectionToDelete.sourceNodeId]?.remove(
      connectionId,
    );
    _connectionsByNodeId[connectionToDelete.targetNodeId]?.remove(
      connectionId,
    );

    // Remove from spatial index
    _spatialIndex.removeConnection(connectionId);
  });

  // Remove cached path to prevent stale rendering
  _connectionPainter?.removeConnectionFromCache(connectionId);

  // Fire event after successful removal
  events.connection?.onDeleted?.call(connectionToDelete);
  // Emit extension event
  _emitEvent(ConnectionRemoved(connectionToDelete));
}