StubViewTree: ensure nodes don't have parents when they're inserted

Summary:
iOS and Android platform code already explicitly check this invariant: nodes cannot have parents when they're inserted into the View hierarchy.

Check this in the core so we get these checks in unit tests, and earlier in the core before platform code runs.

Changelog: [Internal]

Reviewed By: shergin, mdvacca

Differential Revision: D26331842

fbshipit-source-id: c12bc9066d280cb85ccc9e754c9fa475927e6080
This commit is contained in:
Joshua Gross
2021-02-09 22:43:44 -08:00
committed by Facebook GitHub Bot
parent 248704e8de
commit 97ecb5eb19
2 changed files with 7 additions and 0 deletions
@@ -18,6 +18,8 @@
namespace facebook {
namespace react {
static const int NO_VIEW_TAG = -1;
class StubView final {
public:
using Shared = std::shared_ptr<StubView>;
@@ -38,6 +40,7 @@ class StubView final {
LayoutMetrics layoutMetrics;
State::Shared state;
std::vector<StubView::Shared> children;
Tag parentTag{NO_VIEW_TAG};
};
bool operator==(StubView const &lhs, StubView const &rhs);
@@ -90,6 +90,7 @@ void StubViewTree::mutate(ShadowViewMutationList const &mutations) {
auto childTag = mutation.newChildShadowView.tag;
STUB_VIEW_ASSERT(registry.find(childTag) != registry.end());
auto childStubView = registry[childTag];
STUB_VIEW_ASSERT(childStubView->parentTag == NO_VIEW_TAG);
childStubView->update(mutation.newChildShadowView);
STUB_VIEW_LOG({
LOG(ERROR) << "StubView: Insert: " << childTag << " into "
@@ -97,6 +98,7 @@ void StubViewTree::mutate(ShadowViewMutationList const &mutations) {
<< parentStubView->children.size() << " children)";
});
STUB_VIEW_ASSERT(parentStubView->children.size() >= mutation.index);
childStubView->parentTag = parentTag;
parentStubView->children.insert(
parentStubView->children.begin() + mutation.index, childStubView);
break;
@@ -116,6 +118,7 @@ void StubViewTree::mutate(ShadowViewMutationList const &mutations) {
STUB_VIEW_ASSERT(parentStubView->children.size() > mutation.index);
STUB_VIEW_ASSERT(registry.find(childTag) != registry.end());
auto childStubView = registry[childTag];
STUB_VIEW_ASSERT(childStubView->parentTag == parentTag);
bool childIsCorrect =
parentStubView->children.size() > mutation.index &&
parentStubView->children[mutation.index]->tag == childStubView->tag;
@@ -133,6 +136,7 @@ void StubViewTree::mutate(ShadowViewMutationList const &mutations) {
<< ": " << strChildList;
});
STUB_VIEW_ASSERT(childIsCorrect);
childStubView->parentTag = NO_VIEW_TAG;
parentStubView->children.erase(
parentStubView->children.begin() + mutation.index);
break;