Fabric: Simplified ViewShadowNode::cloneAndReplaceChild

Summary: Using methods of the base class instead of custom implementation.

Reviewed By: fkgozali

Differential Revision: D7467795

fbshipit-source-id: 4d168b72880f6900bf8b747e1d655c10140e0c79
This commit is contained in:
Valentin Shergin
2018-04-10 12:45:46 -07:00
committed by Facebook Github Bot
parent a38f32a5a6
commit fec0a51e85

View File

@@ -92,26 +92,14 @@ SharedLayoutableShadowNodeList ViewShadowNode::getChildren() const {
SharedLayoutableShadowNode ViewShadowNode::cloneAndReplaceChild(const SharedLayoutableShadowNode &child) {
ensureUnsealed();
// We cannot mutate `children_` in place here because it is a *shared*
// data structure which means other `ShadowNodes` might refer to its old value.
// So, we have to clone this and only then mutate.
auto nonConstChildrenCopy = SharedShadowNodeList(*children_);
auto viewShadowNodeChild = std::dynamic_pointer_cast<const ViewShadowNode>(child);
assert(viewShadowNodeChild);
auto viewShadowNodeChildClone = std::make_shared<const ViewShadowNode>(viewShadowNodeChild);
std::replace(
nonConstChildrenCopy.begin(),
nonConstChildrenCopy.end(),
std::static_pointer_cast<const ShadowNode>(viewShadowNodeChild),
std::static_pointer_cast<const ShadowNode>(viewShadowNodeChildClone)
);
children_ = std::make_shared<const SharedShadowNodeList>(nonConstChildrenCopy);
ShadowNode::replaceChild(viewShadowNodeChild, viewShadowNodeChildClone);
return std::static_pointer_cast<const LayoutableShadowNode>(viewShadowNodeChildClone);
}
#pragma mark - DebugStringConvertible