Fabric: ShadowTree::completeByReplacingShadowNode

Summary: This method is the core of the future features: `setNativeProps` and `LocalState`.

Reviewed By: sahrens

Differential Revision: D13114789

fbshipit-source-id: 2138496c43c171fe27784b1959d86d6eec4638ee
This commit is contained in:
Valentin Shergin
2018-11-27 18:01:01 -08:00
committed by Facebook Github Bot
parent d594d5a4e5
commit f42d2b9e32
2 changed files with 39 additions and 0 deletions

View File

@@ -98,6 +98,35 @@ bool ShadowTree::complete(
return complete(oldRootShadowNode, newRootShadowNode);
}
bool ShadowTree::completeByReplacingShadowNode(
const SharedShadowNode &oldShadowNode,
const SharedShadowNode &newShadowNode) const {
auto rootShadowNode = getRootShadowNode();
std::vector<std::reference_wrapper<const ShadowNode>> ancestors;
oldShadowNode->constructAncestorPath(*rootShadowNode, ancestors);
if (ancestors.size() == 0) {
return false;
}
auto oldChild = oldShadowNode;
auto newChild = newShadowNode;
SharedShadowNodeUnsharedList sharedChildren;
for (const auto &ancestor : ancestors) {
auto children = ancestor.get().getChildren();
std::replace(children.begin(), children.end(), oldChild, newChild);
sharedChildren = std::make_shared<SharedShadowNodeList>(children);
oldChild = ancestor.get().shared_from_this();
newChild = oldChild->clone(ShadowNodeFragment{.children = sharedChildren});
}
return complete(sharedChildren);
}
bool ShadowTree::complete(
const SharedRootShadowNode &oldRootShadowNode,
const UnsharedRootShadowNode &newRootShadowNode) const {

View File

@@ -77,6 +77,16 @@ class ShadowTree final {
*/
bool complete(const SharedShadowNodeUnsharedList &rootChildNodes) const;
/*
* Replaces a given old shadow node with a new one in the tree by cloning all
* nodes on the path to the root node and then complete the tree.
* Can be called from any thread.
* Returns `true` if the operation finished successfully.
*/
bool completeByReplacingShadowNode(
const SharedShadowNode &oldShadowNode,
const SharedShadowNode &newShadowNode) const;
/*
* Returns a root shadow node that represents the last committed three.
*/