removeNodeById method

void removeNodeById(
  1. String id, {
  2. String? eventId,
  3. bool isHandled = false,
})

This method is used to remove a node by its ID.

Emits a RemoveNodeEvent event.

Implementation

void removeNodeById(
  String id, {
  String? eventId,
  bool isHandled = false,
}) async {
  if (!nodes.containsKey(id)) return;

  final node = nodes[id]!;

  for (final port in node.ports.values) {
    final linksToRemove = port.links.map((link) => link.id).toList();

    for (final linkId in linksToRemove) {
      removeLinkById(linkId, isHandled: true);
    }
  }

  spatialHashGrid.remove(id);
  nodes.remove(id);

  // selectedNodeIds.remove(id); We don't remove the node from the selected nodes because you might be iterating over them.

  // The links data is set to dirty by the removeLinkById method.
  nodesDataDirty = true;

  eventBus.emit(
    RemoveNodeEvent(
      id: eventId ?? const Uuid().v4(),
      node,
      isHandled: isHandled,
    ),
  );
}