From 77c43006d5aca4d4f7afe397d3d223589b6054b6 Mon Sep 17 00:00:00 2001 From: David Alves Date: Fri, 20 Nov 2026 10:49:06 -0800 Subject: [PATCH] Fix Animated re-pushing event-driven props every frame while an animation changes nothing (#59835) Summary: When an animation tick changes no view, `NativeAnimatedNodesManager::pullAnimationMutations` (shared animation backend) and the legacy `onRender` assume an event-driven animation has just finished. They re-run every `Animated.event`-driven value and push all of its props again with `forceFabricCommit`. That condition holds on every frame while any native animation is active but drives no view, so each frame re-applies all event-driven props, posts an empty Fabric commit to the JS thread, and mounts another `UPDATE_PROPS` batch even though nothing is redrawn. This diff runs that re-sync only when an event-driven animation was actually in progress (`isEventAnimationInProgress_`), so it happens once after the last event, as intended. Changelog: [General][Fixed] - Avoid redundant native Animated prop updates while an animation changes no view. Reviewed By: zeyap Differential Revision: D129435409 --- .../animated/NativeAnimatedNodesManager.cpp | 4 +- .../tests/EventAnimationDriverTests.cpp | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp index de87a75b0c5a..dd9976b6c102 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp @@ -1125,7 +1125,7 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations( mutations = onAnimationFrameForBackend(propsBuilder, timestamp, stepAnimations); - if (mutations.batch.empty()) { + if (mutations.batch.empty() && isEventAnimationInProgress_) { // The last animation tick didn't result in any changes to the UI. // It is safe to assume any event animation that was in progress has // completed. @@ -1220,7 +1220,7 @@ void NativeAnimatedNodesManager::onRender() { auto timestamp = static_cast(microseconds) / 1000.0; auto containsChange = onAnimationFrame(timestamp); - if (!containsChange) { + if (!containsChange && isEventAnimationInProgress_) { // The last animation tick didn't result in any changes to the UI. // It is safe to assume any event animation that was in progress has // completed. diff --git a/packages/react-native/ReactCommon/react/renderer/animated/tests/EventAnimationDriverTests.cpp b/packages/react-native/ReactCommon/react/renderer/animated/tests/EventAnimationDriverTests.cpp index d7ffcb9845df..bd0074788f4c 100644 --- a/packages/react-native/ReactCommon/react/renderer/animated/tests/EventAnimationDriverTests.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animated/tests/EventAnimationDriverTests.cpp @@ -175,4 +175,58 @@ TEST_F(EventAnimationDriverTests, sharedBackendEventDoesNotEndDecayAnimation) { EXPECT_GT(animatedValue(), valueAfterEvent); } +TEST_F( + EventAnimationDriverTests, + animationThatChangesNoViewDoesNotRecommitEventDrivenProps) { + initNodesManager(); + nodesManager_->onRender(); + + const folly::dynamic valueNodeConfig = + folly::dynamic::object("type", "value")("value", 0)("offset", 0); + nodesManager_->createAnimatedNode(scrollValueTag_, valueNodeConfig); + nodesManager_->addAnimatedEventToView( + viewTag_, + "scroll", + folly::dynamic::object("animatedValueTag", scrollValueTag_)( + "nativeEventPath", folly::dynamic::array("contentOffset", "y"))); + const auto styleNodeTag = ++tag_; + nodesManager_->createAnimatedNode( + styleNodeTag, + folly::dynamic::object("type", "style")( + "style", folly::dynamic::object("opacity", scrollValueTag_))); + nodesManager_->connectAnimatedNodes(scrollValueTag_, styleNodeTag); + const auto propsNodeTag = ++tag_; + nodesManager_->createAnimatedNode( + propsNodeTag, + folly::dynamic::object("type", "props")( + "props", folly::dynamic::object("style", styleNodeTag))); + nodesManager_->connectAnimatedNodes(styleNodeTag, propsNodeTag); + nodesManager_->connectAnimatedNodeToView(propsNodeTag, viewTag_); + + // A running animation on a value that is connected to no view. + nodesManager_->createAnimatedNode(animatedValueTag_, valueNodeConfig); + nodesManager_->startAnimatingNode( + 1, + animatedValueTag_, + folly::dynamic::object("type", "frames")( + "frames", folly::dynamic::array(0.0, 0.25, 0.5, 0.75, 1.0))( + "toValue", 100)("iterations", -1), + std::nullopt); + + nodesManager_->onRender(); + lastCommittedProps = folly::dynamic::object(); + nodesManager_->onRender(); + nodesManager_->onRender(); + EXPECT_TRUE(lastCommittedProps.empty()); + + // The event-driven props are still committed once after a scroll ends. + dispatchScrollAt(kStartTime, 1); + lastCommittedProps = folly::dynamic::object(); + nodesManager_->onRender(); + EXPECT_EQ(lastCommittedProps["opacity"], 1); + lastCommittedProps = folly::dynamic::object(); + nodesManager_->onRender(); + EXPECT_TRUE(lastCommittedProps.empty()); +} + } // namespace facebook::react