Fabric: RootShadowNode::clone() fix

Summary:
The previous implementation of the method cloned the root node twice (one time at the very end of the method and one time at the end of loop body).
The new one does it once and a bit more readable.

Reviewed By: mdvacca

Differential Revision: D14187969

fbshipit-source-id: 9859deadd4b041ac115c37108188aab70200c75d
This commit is contained in:
Valentin Shergin
2019-02-25 12:21:01 -08:00
committed by Facebook Github Bot
parent 30141e230d
commit af1808de69
@@ -39,29 +39,32 @@ UnsharedRootShadowNode RootShadowNode::clone(
const SharedShadowNode &oldShadowNode,
const SharedShadowNode &newShadowNode) const {
std::vector<std::reference_wrapper<const ShadowNode>> ancestors;
oldShadowNode->constructAncestorPath(*this, ancestors);
if (ancestors.size() == 0) {
if (!oldShadowNode->constructAncestorPath(*this, ancestors)) {
return UnsharedRootShadowNode{nullptr};
}
auto oldChild = oldShadowNode;
auto newChild = newShadowNode;
SharedShadowNodeUnsharedList sharedChildren;
for (const auto &ancestor : ancestors) {
auto children = ancestor.get().getChildren();
auto oldParent = ancestor.get().shared_from_this();
auto children = oldParent->getChildren();
std::replace(children.begin(), children.end(), oldChild, newChild);
sharedChildren = std::make_shared<SharedShadowNodeList>(children);
auto sharedChildren = std::make_shared<SharedShadowNodeList>(children);
auto newParent =
oldParent->clone(ShadowNodeFragment{.children = sharedChildren});
oldChild = ancestor.get().shared_from_this();
newChild = oldChild->clone(ShadowNodeFragment{.children = sharedChildren});
newParent->replaceChild(oldChild, newChild);
oldChild = oldParent;
newChild = newParent;
}
return std::make_shared<RootShadowNode>(
*this, ShadowNodeFragment{.children = sharedChildren});
return std::const_pointer_cast<RootShadowNode>(
std::static_pointer_cast<const RootShadowNode>(newChild));
}
} // namespace react