Fabric: Using ShadowNode::getAncestors in RootShadowNode::clone

Summary: The basic idea of the implementation is the same, but it uses the new method for finding parenting chain and it does not use `shared_from_this()` anymore.

Reviewed By: JoshuaGross

Differential Revision: D14416949

fbshipit-source-id: 13ef928505a60280e2a6c30c76ac87d91cee326e
This commit is contained in:
Valentin Shergin 2019-03-18 23:38:12 -07:00 коммит произвёл Facebook Github Bot
Родитель be51d0564b
Коммит 4602bf5cc6
1 изменённых файлов: 18 добавлений и 21 удалений

Просмотреть файл

@ -44,38 +44,35 @@ UnsharedRootShadowNode RootShadowNode::clone(
}
UnsharedRootShadowNode RootShadowNode::clone(
const SharedShadowNode &oldShadowNode,
const SharedShadowNode &newShadowNode) const {
std::vector<std::reference_wrapper<const ShadowNode>> ancestors;
SharedShadowNode const &oldShadowNode,
SharedShadowNode const &newShadowNode) const {
auto ancestors = oldShadowNode->getAncestors(*this);
if (!oldShadowNode->constructAncestorPath(*this, ancestors)) {
if (ancestors.size() == 0) {
return UnsharedRootShadowNode{nullptr};
}
auto oldChild = oldShadowNode;
auto newChild = newShadowNode;
auto childNode = newShadowNode;
for (const auto &ancestor : ancestors) {
auto oldParent = ancestor.get().shared_from_this();
for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it) {
auto &parentNode = it->first.get();
auto childIndex = it->second;
auto children = oldParent->getChildren();
std::replace(children.begin(), children.end(), oldChild, newChild);
auto children = parentNode.getChildren();
assert(ShadowNode::sameFamily(*children.at(childIndex), *childNode));
children[childIndex] = childNode;
auto sharedChildren = std::make_shared<SharedShadowNodeList>(children);
auto newParent = oldParent->clone({
/* .tag = */ ShadowNodeFragment::tagPlaceholder(),
/* .rootTag = */ ShadowNodeFragment::surfaceIdPlaceholder(),
/* .props = */ ShadowNodeFragment::propsPlaceholder(),
/* .eventEmitter = */ ShadowNodeFragment::eventEmitterPlaceholder(),
/* .children = */ sharedChildren,
childNode = parentNode.clone({
ShadowNodeFragment::tagPlaceholder(),
ShadowNodeFragment::surfaceIdPlaceholder(),
ShadowNodeFragment::propsPlaceholder(),
ShadowNodeFragment::eventEmitterPlaceholder(),
std::make_shared<SharedShadowNodeList>(children),
});
oldChild = oldParent;
newChild = newParent;
}
return std::const_pointer_cast<RootShadowNode>(
std::static_pointer_cast<const RootShadowNode>(newChild));
std::static_pointer_cast<RootShadowNode const>(childNode));
}
} // namespace react