From f7167abce9eb39b7b5b6e5a965d82ae0cdc93ee0 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Thu, 12 Nov 2026 22:25:09 +0000 Subject: [PATCH 1/2] test(fabric): cover native-props cloning and macOS resize Port the merge regression from MIT-licensed react/react-native commit 923e7ddcaf4d2de45ca7dedafbb84c6b87dbffc4 by OrfeasZ (#52646), preserving its license headers. Add native/React precedence and placeholder clone cases, a bounded concurrent clone case, and an in-process AppKit XCTest using the real Scheduler, SurfaceHandler and UIManager. The fixture never accesses the new mutex and can run unchanged against an unpatched baseline. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 33ea5c72-8bbd-448d-aa21-c9b99dd03c96 --- .../uimanager/tests/FabricUIManagerTest.cpp | 182 +++++++++++++++- .../RCTFabricNativePropsTests.mm | 197 ++++++++++++++++++ .../RNTesterPods.xcodeproj/project.pbxproj | 4 + 3 files changed, 380 insertions(+), 3 deletions(-) create mode 100644 packages/rn-tester/RNTester-macOSUnitTests/RCTFabricNativePropsTests.mm 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 c842a454b659..0d36f54cb85e 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/tests/FabricUIManagerTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/tests/FabricUIManagerTest.cpp @@ -5,13 +5,189 @@ * LICENSE file in the root directory of this source tree. */ +// [macOS Adapted from react/react-native@923e7ddcaf4d (#52646). +#include #include +#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 providers{}; + auto registry = providers.createComponentDescriptorRegistry( + {.eventDispatcher = EventDispatcher::Shared{}, + .contextContainer = contextContainer_, + .flavor = nullptr}); + providers.add( + concreteComponentDescriptorProvider()); + providers.add( + concreteComponentDescriptorProvider()); + builder_ = std::make_unique(registry); + + RuntimeExecutor executor = + [](std::function&& /*callback*/) {}; + uiManager_ = std::make_unique(executor, contextContainer_); + uiManager_->setComponentDescriptorRegistry(registry); + + std::shared_ptr root; + // clang-format off + builder_->build( + Element() + .tag(1) + .surfaceId(surfaceId_) + .reference(root) + .props([] { + auto props = std::make_shared(); + props->layoutConstraints = LayoutConstraints{ + .minimumSize = {.width = 0, .height = 0}, + .maximumSize = {.width = 500, .height = 500}}; + return props; + }) + .children({ + Element() + .tag(42) + .surfaceId(surfaceId_) + .props([] { + auto props = std::make_shared(); + props->nativeId = "initial"; + props->opacity = 1.0; + return props; + }) + })); + // clang-format on + + auto tree = std::make_unique( + surfaceId_, + LayoutConstraints{}, + LayoutContext{}, + *uiManager_, + *contextContainer_); + shadowTree_ = tree.get(); + tree->commit([&root](const RootShadowNode&) { return root; }, {true}); + uiManager_->startSurface( + std::move(tree), "test", folly::dynamic::object, DisplayMode::Visible); + } + + void TearDown() override { + uiManager_->stopSurface(surfaceId_); + } + + protected: + std::shared_ptr currentViewNode() const { + return shadowTree_->getCurrentRevision() + .rootShadowNode->getChildren() + .front(); + } + + const ViewProps& viewPropsInTree() const { + return static_cast(*currentViewNode()->getProps()); + } + + static const ViewProps& viewProps(const ShadowNode& node) { + return static_cast(*node.getProps()); + } + + SurfaceId surfaceId_{0}; + std::shared_ptr contextContainer_; + std::unique_ptr builder_; + std::unique_ptr uiManager_; + ShadowTree* shadowTree_{nullptr}; +}; + +TEST_F(FabricUIManagerTest, SetNativePropsMergesWithReactRender) { + ASSERT_EQ(viewPropsInTree().nativeId, "initial"); + ASSERT_FLOAT_EQ(viewPropsInTree().opacity, 1.0); + uiManager_->setNativeProps_DEPRECATED( + currentViewNode(), RawProps(folly::dynamic::object("opacity", 0.5))); + EXPECT_EQ(viewPropsInTree().nativeId, "initial"); + EXPECT_FLOAT_EQ(viewPropsInTree().opacity, 0.5); + + auto rendered = uiManager_->cloneNode( + *currentViewNode(), + nullptr, + RawProps(folly::dynamic::object("nativeID", "from-react"))); + EXPECT_EQ(viewProps(*rendered).nativeId, "from-react"); + EXPECT_FLOAT_EQ(viewProps(*rendered).opacity, 0.5); } + +TEST_F(FabricUIManagerTest, ReactRefreshesNativeOwnedKeysIncludingNull) { + uiManager_->setNativeProps_DEPRECATED( + currentViewNode(), RawProps(folly::dynamic::object("opacity", 0.5))); + auto refreshed = uiManager_->cloneNode( + *currentViewNode(), + nullptr, + RawProps(folly::dynamic::object("opacity", 0.75))); + EXPECT_FLOAT_EQ(viewProps(*refreshed).opacity, 0.75); + EXPECT_FLOAT_EQ(viewProps(*refreshed->clone({})).opacity, 0.75); + + auto reset = uiManager_->cloneNode( + *refreshed, + nullptr, + RawProps(folly::dynamic::object("opacity", nullptr))); + EXPECT_FLOAT_EQ(viewProps(*reset).opacity, 1.0); + EXPECT_FLOAT_EQ(viewProps(*reset->clone({})).opacity, 1.0); +} + +TEST_F(FabricUIManagerTest, ReactOnlyKeysDoNotBecomeNativeOverrides) { + uiManager_->setNativeProps_DEPRECATED( + currentViewNode(), RawProps(folly::dynamic::object("opacity", 0.5))); + auto rendered = uiManager_->cloneNode( + *currentViewNode(), + nullptr, + RawProps(folly::dynamic::object("nativeID", "from-react"))); + auto explicitProps = std::make_shared(); + explicitProps->nativeId = "explicit"; + explicitProps->opacity = 0.9; + auto explicitClone = rendered->clone({.props = explicitProps}); + EXPECT_EQ(viewProps(*explicitClone).nativeId, "explicit"); + EXPECT_FLOAT_EQ(viewProps(*explicitClone).opacity, 0.9); + + auto placeholderClone = explicitClone->clone({}); + EXPECT_EQ(viewProps(*placeholderClone).nativeId, "explicit"); + EXPECT_FLOAT_EQ(viewProps(*placeholderClone).opacity, 0.5); +} + +TEST_F( + FabricUIManagerTest, + ConcurrentReactAndPlaceholderClonesUseOwnedSnapshots) { + uiManager_->setNativeProps_DEPRECATED( + currentViewNode(), RawProps(folly::dynamic::object("opacity", 0.5))); + auto node = currentViewNode(); + std::atomic start{false}; + std::atomic invalidSnapshots{0}; + std::thread writer([&] { + while (!start.load()) { + std::this_thread::yield(); + } + for (int i = 0; i < 1000; ++i) { + uiManager_->cloneNode( + *node, + nullptr, + RawProps(folly::dynamic::object("opacity", i % 2 ? 0.75 : 0.25))); + } + }); + start.store(true); + for (int i = 0; i < 1000; ++i) { + auto clone = node->clone({}); + auto opacity = viewProps(*clone).opacity; + if (opacity != 0.25 && opacity != 0.5 && opacity != 0.75) { + ++invalidSnapshots; + } + } + writer.join(); + EXPECT_EQ(invalidSnapshots.load(), 0); + EXPECT_FLOAT_EQ(viewProps(*node->clone({})).opacity, 0.75); +} + +} // namespace facebook::react +// macOS] diff --git a/packages/rn-tester/RNTester-macOSUnitTests/RCTFabricNativePropsTests.mm b/packages/rn-tester/RNTester-macOSUnitTests/RCTFabricNativePropsTests.mm new file mode 100644 index 000000000000..e8c46373ec24 --- /dev/null +++ b/packages/rn-tester/RNTester-macOSUnitTests/RCTFabricNativePropsTests.mm @@ -0,0 +1,197 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// [macOS] +#import +#import + +#if RCT_NEW_ARCH_ENABLED + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace facebook::react; + +@interface RCTNativePropsResizeDelegate : NSObject +@property (nonatomic, copy) void (^resize)(NSSize); +@end + +@implementation RCTNativePropsResizeDelegate +- (void)windowDidResize:(NSNotification *)notification +{ + NSWindow *window = notification.object; + self.resize(window.contentView.bounds.size); +} +@end + +@interface RCTFabricNativePropsTests : XCTestCase +@end + +@implementation RCTFabricNativePropsTests + +- (void)testWindowResizeWhileUpdatingNativeProps +{ + void (^run)(void) = ^{ + [NSApplication sharedApplication]; + auto context = std::make_shared(); + // The empty surface needs no JavaScript VM, input driver, or mounted views. + RuntimeExecutor executor = [](std::function &&) {}; + auto runtimeScheduler = std::make_shared(executor); + context->insert(RuntimeSchedulerKey, std::weak_ptr(runtimeScheduler)); + auto providers = std::make_shared(); + providers->add(concreteComponentDescriptorProvider()); + providers->add(concreteComponentDescriptorProvider()); + SchedulerToolbox toolbox{ + .contextContainer = context, + .componentRegistryFactory = + [providers]( + const EventDispatcher::Weak &dispatcher, const std::shared_ptr &container) { + return providers->createComponentDescriptorRegistry({dispatcher, container, nullptr}); + }, + .runtimeExecutor = executor, + .eventBeatFactory = + [runtimeScheduler](std::shared_ptr owner) { + return std::make_unique(std::move(owner), *runtimeScheduler); + }}; + Scheduler scheduler(toolbox, nullptr, nullptr); + SurfaceHandler surface("", 11); + scheduler.registerSurface(surface); + surface.constraintLayout( + {.minimumSize = {600, 400}, .maximumSize = {600, 400}, .layoutDirection = LayoutDirection::LeftToRight}, {}); + surface.start(); + auto uiManager = scheduler.getUIManager(); + auto node = uiManager->createNode( + 42, + "View", + 11, + RawProps(folly::dynamic::object("opacity", 0.5)("flex", 1)("nativeID", "resize-target")), + nullptr); + auto children = std::make_shared>>(); + children->push_back(node); + uiManager->completeSurface(11, children, {true}); + uiManager->setNativeProps_DEPRECATED(node, RawProps(folly::dynamic::object("opacity", 0.5))); + + constexpr size_t resizeCount = 120; + constexpr size_t updatesPerResize = 32; + std::mutex gate; + std::condition_variable changed; + size_t requested = 0; + size_t completed = 0; + bool stop = false; + std::atomic timedOut{false}; + std::atomic writes{0}; + std::atomic invalidSnapshots{0}; + auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(20); + + std::thread writer([&] { + for (size_t phase = 1; phase <= resizeCount; ++phase) { + { + std::unique_lock lock(gate); + if (!changed.wait_until(lock, deadline, [&] { return stop || requested >= phase; })) { + timedOut.store(true); + return; + } + if (stop) { + return; + } + } + for (size_t update = 0; update < updatesPerResize; ++update) { + uiManager->setNativeProps_DEPRECATED( + node, RawProps(folly::dynamic::object("opacity", update % 2 ? 0.75 : 0.25))); + ++writes; + } + { + std::lock_guard lock(gate); + completed = phase; + } + changed.notify_one(); + } + }); + + NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 600, 400) + styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskResizable + backing:NSBackingStoreBuffered + defer:NO]; + window.releasedWhenClosed = NO; + RCTNativePropsResizeDelegate *delegate = [RCTNativePropsResizeDelegate new]; + size_t callbacks = 0; + size_t clones = 0; + auto resize = [&](NSSize size) { + ++callbacks; + { + std::lock_guard lock(gate); + requested = callbacks; + } + changed.notify_one(); + surface.constraintLayout( + {.minimumSize = {size.width, size.height}, + .maximumSize = {size.width, size.height}, + .layoutDirection = LayoutDirection::LeftToRight}, + {}); + for (size_t i = 0; i < updatesPerResize; ++i) { + auto clone = node->clone({}); + auto opacity = static_cast(*clone->getProps()).opacity; + if (opacity != 0.25 && opacity != 0.5 && opacity != 0.75) { + ++invalidSnapshots; + } + ++clones; + } + }; + delegate.resize = ^(NSSize size) { + resize(size); + }; + window.delegate = delegate; + for (size_t i = 0; i < resizeCount; ++i) { + [window setContentSize:i % 2 ? NSMakeSize(1000, 740) : NSMakeSize(800, 600)]; + std::unique_lock lock(gate); + if (!changed.wait_until(lock, deadline, [&] { return completed >= i + 1; })) { + timedOut.store(true); + break; + } + } + { + std::lock_guard lock(gate); + stop = true; + } + changed.notify_one(); + writer.join(); + window.delegate = nil; + delegate.resize = nil; + [window close]; + + XCTAssertFalse(timedOut.load()); + XCTAssertEqual(callbacks, resizeCount); + XCTAssertEqual(completed, resizeCount); + XCTAssertEqual(writes.load(), resizeCount * updatesPerResize); + XCTAssertEqual(clones, resizeCount * updatesPerResize); + XCTAssertEqual(invalidSnapshots.load(), 0u); + XCTAssertEqual(surface.getLayoutConstraints().maximumSize.width, 1000); + auto finalClone = node->clone({}); + XCTAssertEqualWithAccuracy(static_cast(*finalClone->getProps()).opacity, 0.75, 0.0001); + surface.stop(); + scheduler.unregisterSurface(surface); + }; + if ([NSThread isMainThread]) { + run(); + } else { + dispatch_sync(dispatch_get_main_queue(), run); + } +} + +@end + +#endif diff --git a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj index 90e6c295ce0e..4c7b46061d23 100644 --- a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj +++ b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + FAB526460000000000000001 /* RCTFabricNativePropsTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = FAB526460000000000000002 /* RCTFabricNativePropsTests.mm */; }; /* [macOS] */ 08E25EBE4A584CD7B70FBB1E /* libPods-RNTester.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D74056A5352F0925816E50E0 /* libPods-RNTester.a */; }; 0CBC915D60C701A81731A5A4 /* libPods-RNTester-macOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C88AE81E635231C7F0F3BA4 /* libPods-RNTester-macOS.a */; }; 0E3E8DF4F87D07566A9489D4 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = AC276E71CF27E40DFA22979F /* PrivacyInfo.xcprivacy */; }; @@ -142,6 +143,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + FAB526460000000000000002 /* RCTFabricNativePropsTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTFabricNativePropsTests.mm; sourceTree = ""; }; /* [macOS] */ 07517909E4081E5D74D5DD60 /* libPods-RNTester-macOSUnitTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RNTester-macOSUnitTests.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 086F285685A842F5E52B6F9C /* Pods-RNTester-visionOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNTester-visionOS.debug.xcconfig"; path = "Target Support Files/Pods-RNTester-visionOS/Pods-RNTester-visionOS.debug.xcconfig"; sourceTree = ""; }; 0EA618022BE537D3001875EF /* RNTesterBundle.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = RNTesterBundle.bundle; path = RNTester/RNTesterBundle.bundle; sourceTree = ""; }; @@ -463,6 +465,7 @@ ACC52F56299ECB8A002A2B0B /* RNTester-macOSUnitTests */ = { isa = PBXGroup; children = ( + FAB526460000000000000002 /* RCTFabricNativePropsTests.mm */, /* [macOS] */ ); path = "RNTester-macOSUnitTests"; sourceTree = ""; @@ -1290,6 +1293,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + FAB526460000000000000001 /* RCTFabricNativePropsTests.mm in Sources */, /* [macOS] */ AC73FCEF29B1318B0003586F /* RCTAnimationUtilsTests.m in Sources */, AC73FD0729B131DA0003586F /* RCTUIManagerTests.m in Sources */, AC73FCF729B131A20003586F /* RCTEventDispatcherTests.m in Sources */, From e4cd75bf0ab058c9c2d3ec195a8d5b22a48b479a Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Thu, 12 Nov 2026 22:28:52 +0000 Subject: [PATCH 2/2] fix(fabric): synchronize native props across shadow node clones Backport MIT-licensed react/react-native commit 923e7ddcaf4d2de45ca7dedafbb84c6b87dbffc4 by OrfeasZ (#52646), preserving upstream copyright and license headers. Use one family-owned mutex for both UIManager writers and ShadowNode cloning, and release it before parsing, descriptor callbacks and shadow-tree commits. Keep merge precedence unchanged and update this branch's existing public C++ API snapshot. Rebuild all native consumers because ShadowNodeFamily layout changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 33ea5c72-8bbd-448d-aa21-c9b99dd03c96 --- .../react/renderer/core/ShadowNode.cpp | 48 +++++--- .../react/renderer/core/ShadowNodeFamily.h | 2 + .../react/renderer/uimanager/UIManager.cpp | 115 ++++++++++-------- scripts/cxx-api/ReactNativeCPP.api | 1 + 4 files changed, 94 insertions(+), 72 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp index 7b894673cdbf..13ad46b4739b 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp @@ -136,32 +136,42 @@ ShadowNode::ShadowNode( } } +// [macOS Backport react/react-native@923e7ddcaf4d (#52646). 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.priops 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); } +// macOS] std::shared_ptr ShadowNode::getContextContainer() const { diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNodeFamily.h index f27b59014dce..3313314275e4 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 // [macOS] Backport upstream #52646. #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; // [macOS] Backport upstream #52646. /** * @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 5582d1312a46..1a3b9c5ff5bb 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp @@ -105,6 +105,7 @@ std::shared_ptr UIManager::createNode( return shadowNode; } +// [macOS Backport react/react-native@923e7ddcaf4d (#52646). std::shared_ptr UIManager::cloneNode( const ShadowNode& shadowNode, const ShadowNode::SharedListOfShared& children, @@ -117,49 +118,51 @@ std::shared_ptr UIManager::cloneNode( auto& componentDescriptor = shadowNode.getComponentDescriptor(); auto& family = shadowNode.getFamily(); - auto props = ShadowNodeFragment::propsPlaceholder(); + 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)); - - // 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)); + 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. + 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( @@ -172,6 +175,7 @@ std::shared_ptr UIManager::cloneNode( return clonedShadowNode; } +// macOS] void UIManager::appendChild( const std::shared_ptr& parentShadowNode, @@ -428,20 +432,25 @@ 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); + // [macOS Backport react/react-native@923e7ddcaf4d (#52646). + { + 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); + } } + // macOS] shadowTreeRegistry_.visit( family.getSurfaceId(), [&](const ShadowTree& shadowTree) { diff --git a/scripts/cxx-api/ReactNativeCPP.api b/scripts/cxx-api/ReactNativeCPP.api index ca72b926a1a8..e5856832e6c0 100644 --- a/scripts/cxx-api/ReactNativeCPP.api +++ b/scripts/cxx-api/ReactNativeCPP.api @@ -29256,6 +29256,7 @@ class ShadowNodeFamily final : public jsi::NativeState { StateUpdate&& stateUpdate, EventQueue::UpdateMode updateMode) const; mutable std::unique_ptr nativeProps_DEPRECATED; + mutable std::mutex nativePropsMutex; Tag getTag() const; jsi::Value getInstanceHandle(jsi::Runtime& runtime) const; InstanceHandle::Shared getInstanceHandle() const;