From af1808de69c36df384b404947f89360f334ddec0 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 25 Feb 2019 12:17:48 -0800 Subject: [PATCH] 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 --- .../fabric/components/root/RootShadowNode.cpp | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ReactCommon/fabric/components/root/RootShadowNode.cpp b/ReactCommon/fabric/components/root/RootShadowNode.cpp index 30d817bc6d2..f10e7ee3ce7 100644 --- a/ReactCommon/fabric/components/root/RootShadowNode.cpp +++ b/ReactCommon/fabric/components/root/RootShadowNode.cpp @@ -39,29 +39,32 @@ UnsharedRootShadowNode RootShadowNode::clone( const SharedShadowNode &oldShadowNode, const SharedShadowNode &newShadowNode) const { std::vector> 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(children); + auto sharedChildren = std::make_shared(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( - *this, ShadowNodeFragment{.children = sharedChildren}); + return std::const_pointer_cast( + std::static_pointer_cast(newChild)); } } // namespace react