diff --git a/packages/react-native/Libraries/Animated/__tests__/Animated-itest.js b/packages/react-native/Libraries/Animated/__tests__/Animated-itest.js index 57b9e696ad5..2093ab8a0a0 100644 --- a/packages/react-native/Libraries/Animated/__tests__/Animated-itest.js +++ b/packages/react-native/Libraries/Animated/__tests__/Animated-itest.js @@ -77,15 +77,14 @@ test('moving box by 100 points', () => { // Animation is completed now. C++ Animated will commit the final position to the shadow tree. if (ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) { - expect(viewElement.getBoundingClientRect().x).toBe(100); - // TODO(T223344928): this shouldn't be neccessary + // TODO(T223344928,T232605345): this shouldn't be neccessary once we disable JS sync and Android's race condition. Fantom.runWorkLoop(); + expect(viewElement.getBoundingClientRect().x).toBe(100); } else { expect(viewElement.getBoundingClientRect().x).toBe(0); Fantom.runWorkLoop(); // Animated still schedules a React state update for synchronisation to shadow tree + expect(viewElement.getBoundingClientRect().x).toBe(100); } - - expect(viewElement.getBoundingClientRect().x).toBe(100); }); test('animation driven by onScroll event', () => { @@ -148,6 +147,10 @@ test('animation driven by onScroll event', () => { expect(transform.translateY).toBeCloseTo(100, 0.001); + // TODO(T232605345): The following two lines won't be necessary once race condition on Android is fixed + expect(viewElement.getBoundingClientRect().y).toBe(0); + Fantom.runWorkLoop(); + expect(viewElement.getBoundingClientRect().y).toBe(100); }); @@ -333,6 +336,8 @@ test('moving box by 50 points with offset 10', () => { ).toBeCloseTo(60, 0.001); if (ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) { + // TODO(T232605345): The following line won't be necessary once race condition on Android is fixed. + Fantom.runWorkLoop(); expect(root.getRenderedOutput({props: ['transform']}).toJSX()).toEqual( , ); @@ -730,27 +735,20 @@ test('Animated.sequence', () => { Fantom.unstable_getDirectManipulationProps(element).transform[0].translateY, ).toBeCloseTo(-16, 0.001); - if (!ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) { - Fantom.runWorkLoop(); // React update to sync end state of 1st timing animation in sequence - } - expect(_isSequenceFinished).toBe(false); - expect(element.getBoundingClientRect().y).toBe(-16); + Fantom.runWorkLoop(); // React update to sync end state of 1st timing animation in sequence - Fantom.runWorkLoop(); // React render + expect(_isSequenceFinished).toBe(false); + + expect(element.getBoundingClientRect().y).toBe(-16); expect( // $FlowFixMe[incompatible-use] Fantom.unstable_getDirectManipulationProps(element).transform[0].translateY, ).toBeCloseTo(0, 0.001); - if (!ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) { - Fantom.runWorkLoop(); // React update to sync end state of 2nd timing animation in sequence - } + Fantom.runWorkLoop(); // React update to sync end state of 2nd timing animation in sequence + expect(element.getBoundingClientRect().y).toBe(0); - if (ReactNativeFeatureFlags.cxxNativeAnimatedRemoveJsSync()) { - expect(_isSequenceFinished).toBe(false); - Fantom.runWorkLoop(); - } expect(_isSequenceFinished).toBe(true); }); diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/AnimatedModule.cpp b/packages/react-native/ReactCxxPlatform/react/renderer/animated/AnimatedModule.cpp index a48a6dbbabc..53278ee2455 100644 --- a/packages/react-native/ReactCxxPlatform/react/renderer/animated/AnimatedModule.cpp +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/AnimatedModule.cpp @@ -284,7 +284,7 @@ void AnimatedModule::executeOperation(const Operation& operation) { void AnimatedModule::installJSIBindingsWithRuntime(jsi::Runtime& runtime) { if (nodesManagerProvider_) { - nodesManager_ = nodesManagerProvider_->getOrCreate(runtime); + nodesManager_ = nodesManagerProvider_->getOrCreate(runtime, jsInvoker_); } } diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/MergedValueDispatcher.cpp b/packages/react-native/ReactCxxPlatform/react/renderer/animated/MergedValueDispatcher.cpp new file mode 100644 index 00000000000..1ef45deca8d --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/MergedValueDispatcher.cpp @@ -0,0 +1,46 @@ +/* + * 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. + */ + +#include "MergedValueDispatcher.h" +#include + +namespace facebook::react { + +MergedValueDispatcher::MergedValueDispatcher( + DispatchFunction dispatchFunction, + MergedValueFunction mergedValueFunction) + : dispatchFunction_(std::move(dispatchFunction)), + mergedValueFunction_(std::move(mergedValueFunction)) {} + +void MergedValueDispatcher::dispatch( + const std::unordered_map& value) { + { + std::lock_guard lock(mutex_); + for (auto& [viewTag, props] : value) { + accumulatedValues_[viewTag] = mergeDynamicProps( + accumulatedValues_[viewTag], props, NullValueStrategy::Override); + } + + if (hasPendingDispatch_) { + return; + } + + hasPendingDispatch_ = true; + } + + dispatchFunction_([this]() { + auto accumulatedValuesCopy = std::unordered_map{}; + { + std::lock_guard lock(mutex_); + std::swap(accumulatedValues_, accumulatedValuesCopy); + hasPendingDispatch_ = false; + } + mergedValueFunction_(std::move(accumulatedValuesCopy)); + }); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/MergedValueDispatcher.h b/packages/react-native/ReactCxxPlatform/react/renderer/animated/MergedValueDispatcher.h new file mode 100644 index 00000000000..c720fda7a8e --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/MergedValueDispatcher.h @@ -0,0 +1,56 @@ +/* + * 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. + */ + +#pragma once + +#include +#include +#include +#include + +namespace facebook::react { + +/** + * A thread-safe dispatcher that ensures only the latest value is dispatched + * to avoid overloading the target thread. Multiple rapid calls are coalesced + * into a single dispatch operation merging the values. + */ +class MergedValueDispatcher { + public: + using DispatchFunction = std::function&&)>; + using MergedValueFunction = + std::function tagToProps)>; + + /** + * Creates a MergedValueDispatcher with the given dispatch function. + * + * @param dispatchFunction - function that dispatches to the target + * thread. + * @param latestValueFunction - function that will be called on the target + * thread with all values merged. + */ + explicit MergedValueDispatcher( + DispatchFunction dispatchFunction, + MergedValueFunction mergedValueFunction); + + /** + * Dispatches the given value. If a dispatch is already pending, this will + * merge with the pending value instead of creating a new dispatch. + * + * @param value - value to be dispatched. + */ + void dispatch(const std::unordered_map& value); + + private: + DispatchFunction dispatchFunction_; + MergedValueFunction mergedValueFunction_; + std::mutex mutex_; + bool hasPendingDispatch_{false}; + std::unordered_map accumulatedValues_; +}; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.cpp b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.cpp index b99169417a2..4e33d8a4717 100644 --- a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.cpp +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace facebook::react { @@ -34,16 +35,28 @@ NativeAnimatedNodesManagerProvider::NativeAnimatedNodesManagerProvider( stopOnRenderCallback_(std::move(stopOnRenderCallback)) {} std::shared_ptr -NativeAnimatedNodesManagerProvider::getOrCreate(jsi::Runtime& runtime) { +NativeAnimatedNodesManagerProvider::getOrCreate( + jsi::Runtime& runtime, + std::shared_ptr jsInvoker) { if (nativeAnimatedNodesManager_ == nullptr) { auto* uiManager = &UIManagerBinding::getBinding(runtime)->getUIManager(); NativeAnimatedNodesManager::FabricCommitCallback fabricCommitCallback = nullptr; + if (!ReactNativeFeatureFlags::disableFabricCommitInCXXAnimated()) { - fabricCommitCallback = - [uiManager](std::unordered_map& tagToProps) { + mergedValueDispatcher_ = std::make_unique( + [jsInvoker](std::function&& func) { + jsInvoker->invokeAsync(std::move(func)); + }, + [uiManager]( + const std::unordered_map& tagToProps) { uiManager->updateShadowTree(tagToProps); + }); + + fabricCommitCallback = + [this](std::unordered_map& tagToProps) { + mergedValueDispatcher_->dispatch(tagToProps); }; } diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.h b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.h index 331dead3b2a..7643825f981 100644 --- a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.h +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManagerProvider.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include "NativeAnimatedNodesManager.h" @@ -35,7 +36,8 @@ class NativeAnimatedNodesManagerProvider { nullptr); std::shared_ptr getOrCreate( - jsi::Runtime& runtime); + jsi::Runtime& runtime, + std::shared_ptr jsInvoker); // Native Event Listeners void addEventEmitterListener( @@ -55,6 +57,8 @@ class NativeAnimatedNodesManagerProvider { NativeAnimatedNodesManager::StartOnRenderCallback startOnRenderCallback_; NativeAnimatedNodesManager::StopOnRenderCallback stopOnRenderCallback_; + + std::unique_ptr mergedValueDispatcher_; }; } // namespace facebook::react