reassemble method

  1. @override
void reassemble()
override

Updates or replaces this ReassemblableNode's children.

Implementation

@override
void reassemble() {
  final oldChildNodes = childNodes;

  final newChildNodes = _sanitizedChildWidgets
      .map((final childWidget) => childWidget.createNode())
      .toList();

  int exactWidgetsSearchStartIndex = 0;
  int matchingWidgetsSearchStartIndex = 0;

  for (final oldChildNode in oldChildNodes) {
    final index = newChildNodes.indexWhere(
      (final newChildNode) => newChildNode.widget == oldChildNode.widget,
      exactWidgetsSearchStartIndex,
    );

    if (index > -1) {
      newChildNodes[index] = oldChildNode;
      exactWidgetsSearchStartIndex = index + 1;
    }
  }

  for (final oldChildNode in oldChildNodes) {
    if (!newChildNodes.contains(oldChildNode)) {
      final index = newChildNodes.indexWhere(
        (final newChildNode) =>
            !oldChildNodes.contains(newChildNode) &&
            newChildNode.widget.matches(oldChildNode.widget),
        matchingWidgetsSearchStartIndex,
      );

      if (index > -1) {
        final newChildNode = newChildNodes[index];

        oldChildNode.widget = newChildNode.widget;
        newChildNodes[index] = oldChildNode;
        matchingWidgetsSearchStartIndex = index + 1;
      }
    }
  }

  for (final childNode in childNodes) {
    if (!newChildNodes.contains(childNode)) childNode.dispose();
  }

  childNodes = newChildNodes;

  for (final childNode in childNodes) {
    if (!oldChildNodes.contains(childNode)) {
      childNode
        ..parentNode = this
        ..initialize();
    }
  }
}