From 2f761ee7c420ce2f595d33dd734cc2a6b13d8b1c Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Mon, 19 May 2025 13:49:27 -0700 Subject: [PATCH] Do not flush Animated operation queue on mount for some feature flags (#51461) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51461 We are trying to minimize the amount of non-determinism in flushing Animated operation queues. Initially the `ReactNativeFeatureFlags.animatedShouldSignalBatch` handled non-determinism on the native side, eliminating the use of native mount hooks to trigger operation batch flushes in the native module. However, there is additional non-determinism introduced by JS, where the set of pending Animated operations may be flushed as a result of an effect. This change eliminates the flushing of Animated operations in the `useEffect` for `createAnimatedPropsHook.js`. ## Changelog [Internal] Reviewed By: yungsters, zeyap Differential Revision: D75003751 fbshipit-source-id: 73c7bb02355b5f634f4a800f46ca5f529cd15ebd --- .../src/private/animated/NativeAnimatedHelper.js | 3 ++- .../src/private/animated/createAnimatedPropsHook.js | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/react-native/src/private/animated/NativeAnimatedHelper.js b/packages/react-native/src/private/animated/NativeAnimatedHelper.js index 3bede66b84e..805e2eb8c22 100644 --- a/packages/react-native/src/private/animated/NativeAnimatedHelper.js +++ b/packages/react-native/src/private/animated/NativeAnimatedHelper.js @@ -57,7 +57,7 @@ const eventListenerAnimationFinishedCallbacks: { let globalEventEmitterGetValueListener: ?EventSubscription = null; let globalEventEmitterAnimationFinishedListener: ?EventSubscription = null; -const shouldSignalBatch = +const shouldSignalBatch: boolean = ReactNativeFeatureFlags.animatedShouldSignalBatch() || ReactNativeFeatureFlags.cxxNativeAnimatedEnabled(); @@ -440,6 +440,7 @@ export default { generateNewAnimationId, assertNativeAnimatedModule, shouldUseNativeDriver, + shouldSignalBatch, transformDataType, // $FlowExpectedError[unsafe-getters-setters] - unsafe getter lint suppression // $FlowExpectedError[missing-type-arg] - unsafe getter lint suppression diff --git a/packages/react-native/src/private/animated/createAnimatedPropsHook.js b/packages/react-native/src/private/animated/createAnimatedPropsHook.js index 222d5a0a6d5..4f76a4fb4cd 100644 --- a/packages/react-native/src/private/animated/createAnimatedPropsHook.js +++ b/packages/react-native/src/private/animated/createAnimatedPropsHook.js @@ -66,9 +66,13 @@ export default function createAnimatedPropsHook( ); useEffect(() => { - // If multiple components call `flushQueue`, the first one will flush the - // queue and subsequent ones will do nothing. - NativeAnimatedHelper.API.flushQueue(); + // Animated queue flush is handled deterministically in setImmediate for the following feature flags: + // animatedShouldSignalBatch, cxxNativeAnimatedEnabled + if (!NativeAnimatedHelper.shouldSignalBatch) { + // If multiple components call `flushQueue`, the first one will flush the + // queue and subsequent ones will do nothing. + NativeAnimatedHelper.API.flushQueue(); + } let drivenAnimationEndedListener: ?EventSubscription = null; if (node.__isNative) { drivenAnimationEndedListener =