diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp index acbc5aea1cf..c147f370735 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp @@ -137,27 +137,35 @@ std::shared_ptr ShadowNode::clone( const ShadowNodeFragment& fragment) const { const auto& family = *family_; const auto& componentDescriptor = family.componentDescriptor_; - if (family.nativeProps_DEPRECATED != nullptr) { - auto propsParserContext = PropsParserContext{family_->getSurfaceId(), {}}; - if (fragment.props == ShadowNodeFragment::propsPlaceholder()) { - // Clone existing `props_` with `family.nativeProps_DEPRECATED` to apply - // previously set props via `setNativeProps` API. - auto props = componentDescriptor.cloneProps( - propsParserContext, props_, RawProps(*family.nativeProps_DEPRECATED)); - auto clonedNode = componentDescriptor.cloneShadowNode( - *this, - {.props = props, - .children = fragment.children, - .state = fragment.state}); - return clonedNode; - } else { - // TODO: We might need to merge fragment.props with - // `family.nativeProps_DEPRECATED`. - return componentDescriptor.cloneShadowNode(*this, fragment); + + std::optional propsOverride; + { + std::lock_guard lock(family.nativePropsMutex); + if (family.nativeProps_DEPRECATED != nullptr) { + if (fragment.props == ShadowNodeFragment::propsPlaceholder()) { + propsOverride.emplace(*family.nativeProps_DEPRECATED); + } else { + // TODO: We might need to merge fragment.props with + // `family.nativeProps_DEPRECATED`. + } } - } else { - return componentDescriptor.cloneShadowNode(*this, fragment); } + + if (propsOverride) { + // Clone existing `props_` with `family.nativeProps_DEPRECATED` to + // apply previously set props via `setNativeProps` API. The parsed + // result escapes the lock so the rest of the clone work runs + // unblocked. + auto propsParserContext = PropsParserContext{family_->getSurfaceId(), {}}; + return componentDescriptor.cloneShadowNode( + *this, + {.props = componentDescriptor.cloneProps( + propsParserContext, props_, std::move(*propsOverride)), + .children = fragment.children, + .state = fragment.state}); + } + + return componentDescriptor.cloneShadowNode(*this, fragment); } std::shared_ptr ShadowNode::getContextContainer() diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h index 2b2ad83dd67..d32b4458c0b 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include @@ -114,6 +115,7 @@ class ShadowNodeFamily final : public jsi::NativeState { * architecture and will be removed in the future. */ mutable std::unique_ptr nativeProps_DEPRECATED; + mutable std::mutex nativePropsMutex; /** * @return tag for the ShadowNodeFamily. diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp index 4240a94ea4c..16aa4ac40d1 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp @@ -118,49 +118,51 @@ std::shared_ptr UIManager::cloneNode( auto& componentDescriptor = shadowNode.getComponentDescriptor(); auto& family = shadowNode.getFamily(); + auto props = ShadowNodeFragment::propsPlaceholder(); - if (!rawProps.isEmpty()) { - if (family.nativeProps_DEPRECATED != nullptr) { - // 1. update the nativeProps_DEPRECATED props. - // - // In this step, we want the most recent value for the props - // managed by setNativeProps. - // Values in `rawProps` patch (take precedence over) - // `nativeProps_DEPRECATED`. For example, if both - // `nativeProps_DEPRECATED` and `rawProps` contain key 'A'. - // Value from `rawProps` overrides what was previously in - // `nativeProps_DEPRECATED`. Notice that the `nativeProps_DEPRECATED` - // patch will not get more props from `rawProps`: if the key is not - // present in `nativeProps_DEPRECATED`, it will not be added. - // - // The result of this operation is the new `nativeProps_DEPRECATED`. - family.nativeProps_DEPRECATED = - std::make_unique(mergeDynamicProps( - *family.nativeProps_DEPRECATED, // source - (folly::dynamic)rawProps, // patch - NullValueStrategy::Ignore)); + std::optional finalProps; + { + std::lock_guard lock(family.nativePropsMutex); + if (family.nativeProps_DEPRECATED != nullptr) { + // 1. update the nativeProps_DEPRECATED props. + // + // In this step, we want the most recent value for the props + // managed by setNativeProps. + // Values in `rawProps` patch (take precedence over) + // `nativeProps_DEPRECATED`. For example, if both + // `nativeProps_DEPRECATED` and `rawProps` contain key 'A'. + // Value from `rawProps` overrides what was previously in + // `nativeProps_DEPRECATED`. Notice that the `nativeProps_DEPRECATED` + // patch will not get more props from `rawProps`: if the key is not + // present in `nativeProps_DEPRECATED`, it will not be added. + // + // The result of this operation is the new `nativeProps_DEPRECATED`. + family.nativeProps_DEPRECATED = + std::make_unique(mergeDynamicProps( + *family.nativeProps_DEPRECATED, // source + (folly::dynamic)rawProps, // patch + NullValueStrategy::Ignore)); - // 2. Compute the final set of props. - // - // This step takes the new props handled by `setNativeProps` and - // merges them in the `rawProps` managed by React. - // The new props handled by `nativeProps` now takes precedence - // on the props handled by React, as we want to make sure that - // all the props are applied to the component. - // We use these finalProps as source of truth for the component. - auto finalProps = mergeDynamicProps( - (folly::dynamic)rawProps, // source - *family.nativeProps_DEPRECATED, // patch - NullValueStrategy::Override); - - // 3. Clone the props by using finalProps. - props = componentDescriptor.cloneProps( - propsParserContext, shadowNode.getProps(), RawProps(finalProps)); - } else { - props = componentDescriptor.cloneProps( - propsParserContext, shadowNode.getProps(), std::move(rawProps)); + // 2. Compute the final set of props. + // + // This step takes the new props handled by `setNativeProps` and + // merges them in the `rawProps` managed by React. + // The new props handled by `nativeProps` now takes precedence + // on the props handled by React, as we want to make sure that + // all the props are applied to the component. + // We use these finalProps as source of truth for the component. + finalProps = mergeDynamicProps( + (folly::dynamic)rawProps, // source + *family.nativeProps_DEPRECATED, // patch + NullValueStrategy::Override); + } } + + props = componentDescriptor.cloneProps( + propsParserContext, + shadowNode.getProps(), + finalProps ? RawProps(std::move(*finalProps)) : std::move(rawProps)); } auto clonedShadowNode = componentDescriptor.cloneShadowNode( @@ -448,19 +450,22 @@ void UIManager::setNativeProps_DEPRECATED( const std::shared_ptr& shadowNode, RawProps rawProps) const { auto& family = shadowNode->getFamily(); - if (family.nativeProps_DEPRECATED) { - // Values in `rawProps` patch (take precedence over) - // `nativeProps_DEPRECATED`. For example, if both `nativeProps_DEPRECATED` - // and `rawProps` contain key 'A'. Value from `rawProps` overrides what - // was previously in `nativeProps_DEPRECATED`. - family.nativeProps_DEPRECATED = - std::make_unique(mergeDynamicProps( - *family.nativeProps_DEPRECATED, - (folly::dynamic)rawProps, - NullValueStrategy::Override)); - } else { - family.nativeProps_DEPRECATED = - std::make_unique((folly::dynamic)rawProps); + { + std::lock_guard lock(family.nativePropsMutex); + if (family.nativeProps_DEPRECATED) { + // Values in `rawProps` patch (take precedence over) + // `nativeProps_DEPRECATED`. For example, if both + // `nativeProps_DEPRECATED` and `rawProps` contain key 'A'. Value from + // `rawProps` overrides what was previously in `nativeProps_DEPRECATED`. + family.nativeProps_DEPRECATED = + std::make_unique(mergeDynamicProps( + *family.nativeProps_DEPRECATED, + (folly::dynamic)rawProps, + NullValueStrategy::Override)); + } else { + family.nativeProps_DEPRECATED = + std::make_unique((folly::dynamic)rawProps); + } } shadowTreeRegistry_.visit( diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/tests/FabricUIManagerTest.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/tests/FabricUIManagerTest.cpp index c842a454b65..47aaa20ef56 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/tests/FabricUIManagerTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/tests/FabricUIManagerTest.cpp @@ -8,10 +8,151 @@ #include #include +#include +#include +#include +#include #include -using namespace facebook::react; +namespace facebook::react { -TEST(UIManagerTest, testSomething) { - // TODO +class FabricUIManagerTest : public ::testing::Test { + public: + FabricUIManagerTest() { + contextContainer_ = std::make_shared(); + + ComponentDescriptorProviderRegistry componentDescriptorProviderRegistry{}; + auto componentDescriptorRegistry = + componentDescriptorProviderRegistry.createComponentDescriptorRegistry( + ComponentDescriptorParameters{ + .eventDispatcher = EventDispatcher::Shared{}, + .contextContainer = contextContainer_, + .flavor = nullptr}); + + componentDescriptorProviderRegistry.add( + concreteComponentDescriptorProvider()); + componentDescriptorProviderRegistry.add( + concreteComponentDescriptorProvider()); + + builder_ = std::make_unique(componentDescriptorRegistry); + + RuntimeExecutor runtimeExecutor = + [](std::function&& /*callback*/) {}; + uiManager_ = + std::make_unique(runtimeExecutor, contextContainer_); + uiManager_->setComponentDescriptorRegistry(componentDescriptorRegistry); + + buildAndCommitTree(); + } + + void TearDown() override { + uiManager_->stopSurface(surfaceId_); + } + + protected: + std::shared_ptr buildTree() { + std::shared_ptr rootNode; + + // clang-format off + auto element = + Element() + .tag(1) + .surfaceId(surfaceId_) + .reference(rootNode) + .props([] { + auto sharedProps = std::make_shared(); + sharedProps->layoutConstraints = LayoutConstraints{ + .minimumSize = {.width = 0, .height = 0}, + .maximumSize = {.width = 500, .height = 500}}; + return sharedProps; + }) + .children({ + Element() + .tag(viewTag_) + .surfaceId(surfaceId_) + .props([] { + auto sharedProps = std::make_shared(); + sharedProps->nativeId = "initial"; + sharedProps->opacity = 1.0; + return sharedProps; + }) + }); + // clang-format on + + builder_->build(element); + return rootNode; + } + + void buildAndCommitTree() { + auto rootNode = buildTree(); + + auto shadowTree = std::make_unique( + surfaceId_, + LayoutConstraints{}, + LayoutContext{}, + *uiManager_, + *contextContainer_); + + shadowTreePtr_ = shadowTree.get(); + + shadowTree->commit( + [&rootNode](const RootShadowNode& /*oldRootShadowNode*/) { + return std::static_pointer_cast(rootNode); + }, + {true}); + + uiManager_->startSurface( + std::move(shadowTree), + "test", + folly::dynamic::object, + DisplayMode::Visible); + } + + std::shared_ptr currentViewNode() const { + auto root = shadowTreePtr_->getCurrentRevision().rootShadowNode; + return root->getChildren().front(); + } + + const ViewProps& viewPropsInTree() const { + return static_cast(*currentViewNode()->getProps()); + } + + SurfaceId surfaceId_{0}; + Tag viewTag_{42}; + std::shared_ptr contextContainer_; + std::unique_ptr builder_; + std::unique_ptr uiManager_; + ShadowTree* shadowTreePtr_{nullptr}; +}; + +// Demonstrates the merge between props set by `setNativeProps_DEPRECATED` +// (the legacy native-side prop override path) and props supplied by a +// regular React re-render via `cloneNode`. The native override must +// survive a React render that does not touch the overridden key, and +// other React-supplied keys must still apply. +TEST_F(FabricUIManagerTest, SetNativePropsMergesWithReactRender) { + ASSERT_EQ(viewPropsInTree().nativeId, "initial"); + ASSERT_FLOAT_EQ(viewPropsInTree().opacity, 1.0); + + // Native override on `opacity` only. The committed tree picks up the new + // opacity and leaves the unrelated `nativeId` untouched. + uiManager_->setNativeProps_DEPRECATED( + currentViewNode(), RawProps(folly::dynamic::object("opacity", 0.5))); + EXPECT_EQ(viewPropsInTree().nativeId, "initial"); + EXPECT_FLOAT_EQ(viewPropsInTree().opacity, 0.5); + + // Simulate a React re-render that updates `nativeID` only. The native + // `opacity` override must survive (it is not present in rawProps and + // therefore not refreshed away by React), and the new `nativeID` must + // apply on top. + auto rerendered = uiManager_->cloneNode( + *currentViewNode(), + /*children=*/nullptr, + RawProps(folly::dynamic::object("nativeID", "from-react"))); + auto& merged = static_cast(*rerendered->getProps()); + EXPECT_EQ(merged.nativeId, "from-react"); + EXPECT_FLOAT_EQ(merged.opacity, 0.5); } + +} // namespace facebook::react diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index 75096bdf1b5..10e8e967b09 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -4660,6 +4660,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index 437a3d183c9..f0e83f12678 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -4474,6 +4474,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index 5fc5fc7a257..ddc02c6142f 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -4651,6 +4651,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index a9852878ba9..8b3b853e64b 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -6861,6 +6861,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 980c2ec4f9a..6542ed5c733 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -6703,6 +6703,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 2a0f566a361..79388386ce7 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -6852,6 +6852,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index a241ca4acd5..1e6c19dd250 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -3214,6 +3214,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api index 377d65bb4fb..3295c1fa8d4 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api @@ -3068,6 +3068,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr; diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index ac2a39e1801..50cbb2a37a6 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -3205,6 +3205,7 @@ class facebook::react::ShadowNodeFamily : public facebook::jsi::NativeState { public facebook::react::SharedEventEmitter getEventEmitter() const; public facebook::react::SurfaceId getSurfaceId() const; public facebook::react::Tag getTag() const; + public mutable std::mutex nativePropsMutex; public std::shared_ptr getMostRecentState() const; public using AncestorList = std::vector, int>>; public using Shared = std::shared_ptr;