1010namespace facebook {
1111namespace react {
1212
13- using ChangedShadowNodePairs =
14- std::vector<std::pair<SharedShadowNode, UnsharedShadowNode >>;
13+ using ChangedShadowNodePairs = std::vector<
14+ std::pair<ShadowNode::Shared const &, ShadowNode::Unshared const & >>;
1515
1616/* *
1717 * Clones any children in the subtree that need to be cloned, and adds those to
1818 * the `changedPairs` vector argument.
19- *
20- * @param parent
21- * @param newChildren
22- * @param oldChildren
23- * @param changedPairs
2419 */
25- static void reconcileStateWithChildren (
26- ShadowNode const *parent,
27- const SharedShadowNodeList &newChildren,
28- const SharedShadowNodeList &oldChildren,
29- ChangedShadowNodePairs &changedPairs) {
20+ static ChangedShadowNodePairs reconcileStateWithChildren (
21+ SharedShadowNodeList const &newChildren,
22+ SharedShadowNodeList const &oldChildren) {
23+ ChangedShadowNodePairs changedPairs;
3024 // Find children that are the same family in both trees.
3125 // We only want to find nodes that existing in the new tree - if they
3226 // don't exist in the new tree, they're being deleted; if they don't exist
@@ -35,19 +29,22 @@ static void reconcileStateWithChildren(
3529 // Currently we use a naive double loop - this could be improved, but we need
3630 // to be able to handle cases where nodes are entirely reordered, for
3731 // instance.
38- for (int i = 0 ; i < newChildren.size (); i++) {
39- bool found = false ;
40- for (int j = 0 ; j < oldChildren.size () && !found; j++) {
41- if (ShadowNode::sameFamily (*newChildren[i], *oldChildren[j])) {
42- UnsharedShadowNode newChild =
43- reconcileStateWithTree (newChildren[i].get (), oldChildren[j]);
44- if (newChild != nullptr ) {
45- changedPairs.push_back (std::make_pair (newChildren[i], newChild));
46- }
47- found = true ;
32+ for (auto const &child : newChildren) {
33+ auto const oldChild = std::find_if (
34+ oldChildren.begin (), oldChildren.end (), [&](const auto &el) {
35+ return ShadowNode::sameFamily (*child, *el);
36+ });
37+
38+ if (oldChild != oldChildren.end ()) {
39+ UnsharedShadowNode newChild =
40+ reconcileStateWithTree (child.get (), *oldChild);
41+ if (newChild != nullptr ) {
42+ changedPairs.push_back (std::make_pair (child, newChild));
4843 }
4944 }
50- }
45+ };
46+
47+ return changedPairs;
5148}
5249
5350UnsharedShadowNode reconcileStateWithTree (
@@ -63,24 +60,24 @@ UnsharedShadowNode reconcileStateWithTree(
6360 // and/or 2) some descendant node is out-of-date and must be reconciled.
6461 // This requires traversing all children, and we must at *least* clone
6562 // this node, whether or not we clone and update any children.
66- const auto &newChildren = newNode->getChildren ();
67- const auto &oldChildren = committedNode->getChildren ();
68- ChangedShadowNodePairs changedPairs;
69- reconcileStateWithChildren (newNode, newChildren, oldChildren, changedPairs );
63+ auto const &newChildren = newNode->getChildren ();
64+ auto const &oldChildren = committedNode->getChildren ();
65+ auto const changedPairs =
66+ reconcileStateWithChildren (newChildren, oldChildren);
7067
7168 ShadowNode::SharedListOfShared clonedChildren =
7269 ShadowNodeFragment::childrenPlaceholder ();
7370
7471 // If any children were cloned, we need to recreate the child list.
7572 // This won't cause any children to be cloned that weren't already cloned -
7673 // it just collects all children, cloned or uncloned, into a new list.
77- if (changedPairs.size () > 0 ) {
74+ if (! changedPairs.empty () ) {
7875 ShadowNode::UnsharedListOfShared newList =
7976 std::make_shared<ShadowNode::ListOfShared>();
80- for (int i = 0 , j = 0 ; i < newChildren.size (); i++ ) {
77+ for (std:: size_t i = 0 , j = 0 ; i < newChildren.size (); ++i ) {
8178 if (j < changedPairs.size () && changedPairs[j].first == newChildren[i]) {
8279 newList->push_back (changedPairs[j].second );
83- j++ ;
80+ ++j ;
8481 } else {
8582 newList->push_back (newChildren[i]);
8683 }
0 commit comments