Skip to content

Commit acdaf51

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Apply small style adjustments to reconcileStateWithTree
Summary: Changelog: [Internal] There shouldn't be any logical changes to the code. Reviewed By: JoshuaGross Differential Revision: D19814500 fbshipit-source-id: 2be492eebb284c7e241662d9d78117badcb2eee4
1 parent bfa00a5 commit acdaf51

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

ReactCommon/fabric/core/shadownode/ShadowNode.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ bool ShadowNode::sameFamily(const ShadowNode &first, const ShadowNode &second) {
2828
return first.family_ == second.family_;
2929
}
3030

31-
#pragma mark - Constructors
32-
3331
static int computeStateRevision(
34-
const State::Shared &state,
35-
const SharedShadowNodeSharedList &children) {
32+
State::Shared const &state,
33+
SharedShadowNodeSharedList const &children) {
3634
int fragmentStateRevision = state ? state->getRevision() : 0;
3735
int childrenSum = 0;
3836

3937
if (children) {
40-
for (const auto &child : *children) {
38+
for (auto const &child : *children) {
4139
childrenSum += child->getStateRevision();
4240
}
4341
}
4442

4543
return fragmentStateRevision + childrenSum;
4644
}
4745

46+
#pragma mark - Constructors
47+
4848
ShadowNode::ShadowNode(
4949
ShadowNodeFragment const &fragment,
5050
ShadowNodeFamily::Shared const &family,

ReactCommon/fabric/mounting/TreeStateReconciliation.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,17 @@
1010
namespace facebook {
1111
namespace 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

5350
UnsharedShadowNode 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

Comments
 (0)