Introduce new trait: ClonedByNativeStateUpdate (#44018)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44018

changelog: [internal]

New trait ClonedByNativeStateUpdate is used to mark the path that was cloned by native state update.

This is a pre-requisite for new state reconciliation algorithm. It will mark part of shadow tree that was affected by native state update.

Reviewed By: rubennorte

Differential Revision: D55922776

fbshipit-source-id: 6d4515460346c341af3ee6117d570b3201328bc9
This commit is contained in:
Samuel Susla
2024-04-12 02:47:01 -07:00
committed by Facebook GitHub Bot
parent 3b49db3bfe
commit 669aa1c3c2
3 changed files with 23 additions and 13 deletions
@@ -107,6 +107,9 @@ ShadowNode::ShadowNode(
react_native_assert(props_);
react_native_assert(children_);
// State could have been progressed above by checking
// `sourceShadowNode.getMostRecentState()`.
traits_.unset(ShadowNodeTraits::Trait::ClonedByNativeStateUpdate);
traits_.set(ShadowNodeTraits::Trait::ChildrenAreShared);
traits_.set(fragment.traits.get());
@@ -70,7 +70,8 @@ class ShadowNodeTraits {
// to be cloned before the first mutation.
ChildrenAreShared = 1 << 8,
Reserved = 1 << 31,
// Indicates that the node was cloned because of native state update.
ClonedByNativeStateUpdate = 1 << 9,
};
/*
@@ -216,15 +216,20 @@ TEST_F(ShadowNodeTest, handleCloningWithTraits) {
auto clonedWithoutTraits = nodeAB_->clone({});
EXPECT_FALSE(clonedWithoutTraits->getTraits().check(
ShadowNodeTraits::Trait::Reserved));
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
auto newTraits = ShadowNodeTraits();
newTraits.set(ShadowNodeTraits::Trait::Reserved);
newTraits.set(ShadowNodeTraits::Trait::ClonedByNativeStateUpdate);
auto clonedWithTraits = clonedWithoutTraits->clone({.traits = newTraits});
EXPECT_TRUE(
clonedWithTraits->getTraits().check(ShadowNodeTraits::Trait::Reserved));
EXPECT_TRUE(clonedWithTraits->getTraits().check(
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
auto clonedAgain = clonedWithTraits->clone({});
EXPECT_FALSE(clonedAgain->getTraits().check(
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
}
TEST_F(ShadowNodeTest, handleState) {
@@ -285,7 +290,7 @@ TEST_F(ShadowNodeTest, handleState) {
TEST_F(ShadowNodeTest, testCloneTree) {
auto& family = nodeABA_->getFamily();
auto newTraits = ShadowNodeTraits();
newTraits.set(ShadowNodeTraits::Trait::Reserved);
newTraits.set(ShadowNodeTraits::Trait::ClonedByNativeStateUpdate);
auto rootNode = nodeA_->cloneTree(
family,
[newTraits](ShadowNode const& oldShadowNode) {
@@ -293,21 +298,22 @@ TEST_F(ShadowNodeTest, testCloneTree) {
},
newTraits);
EXPECT_TRUE(rootNode->getTraits().check(ShadowNodeTraits::Trait::Reserved));
EXPECT_TRUE(rootNode->getTraits().check(
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
EXPECT_FALSE(rootNode->getChildren()[0]->getTraits().check(
ShadowNodeTraits::Trait::Reserved));
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
auto const& firstLevelChild = *rootNode->getChildren()[1];
EXPECT_TRUE(
firstLevelChild.getTraits().check(ShadowNodeTraits::Trait::Reserved));
EXPECT_TRUE(firstLevelChild.getTraits().check(
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
EXPECT_FALSE(firstLevelChild.getChildren()[1]->getTraits().check(
ShadowNodeTraits::Trait::Reserved));
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
auto const& secondLevelchild = *firstLevelChild.getChildren()[0];
EXPECT_TRUE(
secondLevelchild.getTraits().check(ShadowNodeTraits::Trait::Reserved));
EXPECT_TRUE(secondLevelchild.getTraits().check(
ShadowNodeTraits::Trait::ClonedByNativeStateUpdate));
}