deserializeNodes method

Map<String, VSNodeData> deserializeNodes(
  1. String dataString
)

Deserializes data in two steps:

  1. builds new nodes with position and id from saved data
  2. reconstruct connections between the nodes

Returns a Map<NodeId,VSNodeData>

Implementation

Map<String, VSNodeData> deserializeNodes(String dataString) {
  final data = jsonDecode(dataString) as Map<String, dynamic>;

  final Map<String, VSNodeData> decoded = {};

  data.forEach((key, value) {
    final node = _nodeBuilders[value["type"]]?.call(Offset.zero, null);

    if (node == null) {
      //ignore: avoid_print
      print(
        "A node was serialized but the builder for its type is missing.\nIt will be remove from the current node tree.\n$value",
      );
      onBuilderMissing?.call(value);
      return;
    }

    node.setBaseData(
      value["id"],
      value["title"],
      offsetFromJson(value["widgetOffset"]),
    );

    if (value["value"] != null) {
      (node as VSWidgetNode).setValue(value["value"]);
    }

    decoded[key] = node;
  });

  data.forEach((key, value) {
    final inputData = value["inputData"] as List<dynamic>;
    final Map<String, VSOutputData?> inputRefs = {};

    for (final element in inputData) {
      final serializedOutput = element["connectedNode"];

      if (serializedOutput != null) {
        final refOutput =
            decoded[serializedOutput["nodeData"]]?.outputData.firstWhere(
                  (element) => element.type == serializedOutput["name"],
                );
        inputRefs[element["name"]] = refOutput;
      }
    }

    decoded[key]?.setRefData(inputRefs);
  });

  return decoded;
}