forTree static method
Implementation
@visibleForTesting
static TreeDiff forTree(
{required TreeNode oldTree, required TreeNode newTree}) {
final oldKeys = oldTree.children.keys.toSet();
final newKeys = newTree.children.keys.toSet();
final nodesAdded = newKeys.difference(oldKeys).map((nodeKey) {
final node = newTree.children[nodeKey];
return node == null ? null : TreeDiffNodeAdd(node);
}).filterNotNull();
final nodesRemoved = oldKeys.difference(newKeys).map((nodeKey) {
final node = oldTree.children[nodeKey];
return node == null ? null : TreeDiffNodeRemove(data: node);
}).filterNotNull();
final nodesUnchanged = <(TreeNode, TreeNode)>[];
final nodesUpdated = <TreeDiffNodeUpdate>[];
for (final nodeKey in oldKeys.intersection(newKeys)) {
if ((oldTree.children[nodeKey]! as TreeNode).data !=
(newTree.children[nodeKey]! as TreeNode).data) {
nodesUpdated.add(TreeDiffNodeUpdate(newTree.children[nodeKey]!));
}
nodesUnchanged.add(
(
oldTree.children[nodeKey]! as TreeNode,
newTree.children[nodeKey]! as TreeNode,
),
);
}
return TreeDiff._(
oldTree: oldTree,
newTree: newTree,
nodesAdded: nodesAdded,
nodesRemoved: nodesRemoved,
nodesUnchanged: nodesUnchanged,
nodesUpdated: nodesUpdated,
);
}