reduce method

  1. @override
GraphState reduce()

The method that returns the new state.

Implementation

@override
GraphState reduce() {
  final nodesDto = nodes.map((e) => GraphNodeDto.fromJson(e)).toList();
  final result = <int, InputNode>{}; // id -> node
  for (final dto in nodesDto) {
    result[dto.id] = InputNode(
      type: dto.type,
      label: dto.debugLabel,
    );
  }

  // Assign edges
  for (final dto in nodesDto) {
    final inputNode = result[dto.id]!;
    for (final parentId in dto.parents) {
      final parentNode = result[parentId]!;
      inputNode.parents.add(parentNode);
    }
    for (final childId in dto.children) {
      final childNode = result[childId]!;
      inputNode.children.add(childNode);
    }
  }

  return state.copyWith(nodes: result.values.toList());
}