initializeTracking method

void initializeTracking()

Initializes tracking state from the current tree controller.

Call after construction when the tree controller already has nodes (e.g., when recreating the sync controller mid-lifetime). Without this, the first syncRoots call treats all existing nodes as new and cannot remove nodes that are no longer desired.

Implementation

void initializeTracking() {
  _currentRoots
    ..clear()
    ..addAll(_controller.rootKeys);
  _currentChildren.clear();

  // Iterative DFS so deep linear chains do not stack-overflow Dart's
  // recursion limit (typically ~10k–20k frames).
  final stack = <TKey>[..._currentRoots];
  while (stack.isNotEmpty) {
    final key = stack.removeLast();
    final children = _controller.getChildren(key);
    _currentChildren[key] = List<TKey>.of(children);
    for (final childKey in children) {
      stack.add(childKey);
    }
  }
}