Skip to content

Commit de3afe5

Browse files
David Alvesfacebook-github-bot
authored andcommitted
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
1 parent 2c52501 commit de3afe5

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations(
11251125
mutations =
11261126
onAnimationFrameForBackend(propsBuilder, timestamp, stepAnimations);
11271127

1128-
if (mutations.batch.empty()) {
1128+
if (mutations.batch.empty() && isEventAnimationInProgress_) {
11291129
// The last animation tick didn't result in any changes to the UI.
11301130
// It is safe to assume any event animation that was in progress has
11311131
// completed.
@@ -1220,7 +1220,7 @@ void NativeAnimatedNodesManager::onRender() {
12201220
auto timestamp = static_cast<double>(microseconds) / 1000.0;
12211221
auto containsChange = onAnimationFrame(timestamp);
12221222

1223-
if (!containsChange) {
1223+
if (!containsChange && isEventAnimationInProgress_) {
12241224
// The last animation tick didn't result in any changes to the UI.
12251225
// It is safe to assume any event animation that was in progress has
12261226
// completed.

‎packages/react-native/ReactCommon/react/renderer/animated/tests/EventAnimationDriverTests.cpp‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,58 @@ TEST_F(EventAnimationDriverTests, sharedBackendEventDoesNotEndDecayAnimation) {
175175
EXPECT_GT(animatedValue(), valueAfterEvent);
176176
}
177177

178+
TEST_F(
179+
EventAnimationDriverTests,
180+
animationThatChangesNoViewDoesNotRecommitEventDrivenProps) {
181+
initNodesManager();
182+
nodesManager_->onRender();
183+
184+
const folly::dynamic valueNodeConfig =
185+
folly::dynamic::object("type", "value")("value", 0)("offset", 0);
186+
nodesManager_->createAnimatedNode(scrollValueTag_, valueNodeConfig);
187+
nodesManager_->addAnimatedEventToView(
188+
viewTag_,
189+
"scroll",
190+
folly::dynamic::object("animatedValueTag", scrollValueTag_)(
191+
"nativeEventPath", folly::dynamic::array("contentOffset", "y")));
192+
const auto styleNodeTag = ++tag_;
193+
nodesManager_->createAnimatedNode(
194+
styleNodeTag,
195+
folly::dynamic::object("type", "style")(
196+
"style", folly::dynamic::object("opacity", scrollValueTag_)));
197+
nodesManager_->connectAnimatedNodes(scrollValueTag_, styleNodeTag);
198+
const auto propsNodeTag = ++tag_;
199+
nodesManager_->createAnimatedNode(
200+
propsNodeTag,
201+
folly::dynamic::object("type", "props")(
202+
"props", folly::dynamic::object("style", styleNodeTag)));
203+
nodesManager_->connectAnimatedNodes(styleNodeTag, propsNodeTag);
204+
nodesManager_->connectAnimatedNodeToView(propsNodeTag, viewTag_);
205+
206+
// A running animation on a value that is connected to no view.
207+
nodesManager_->createAnimatedNode(animatedValueTag_, valueNodeConfig);
208+
nodesManager_->startAnimatingNode(
209+
1,
210+
animatedValueTag_,
211+
folly::dynamic::object("type", "frames")(
212+
"frames", folly::dynamic::array(0.0, 0.25, 0.5, 0.75, 1.0))(
213+
"toValue", 100)("iterations", -1),
214+
std::nullopt);
215+
216+
nodesManager_->onRender();
217+
lastCommittedProps = folly::dynamic::object();
218+
nodesManager_->onRender();
219+
nodesManager_->onRender();
220+
EXPECT_TRUE(lastCommittedProps.empty());
221+
222+
// The event-driven props are still committed once after a scroll ends.
223+
dispatchScrollAt(kStartTime, 1);
224+
lastCommittedProps = folly::dynamic::object();
225+
nodesManager_->onRender();
226+
EXPECT_EQ(lastCommittedProps["opacity"], 1);
227+
lastCommittedProps = folly::dynamic::object();
228+
nodesManager_->onRender();
229+
EXPECT_TRUE(lastCommittedProps.empty());
230+
}
231+
178232
} // namespace facebook::react

0 commit comments

Comments
 (0)