From 63100a9639f7d4eed5892e66ef25ef96d9e76d0a Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Wed, 2 Oct 2024 02:07:00 -0700 Subject: [PATCH] Animated: Simplify `TimingAnimation#start` Logic (#46719) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46719 Implements a minor refactor of `TimingAnimation#start` so that we standardize how the `_useNativeDriver` case is handled. There is no behavior change from this besides more consistently assigning to `this._startTime`. (Previously, we would not set it if the duration were 0 and `!useNativeDriver`.) Changelog: [Internal] Reviewed By: javache Differential Revision: D63572066 fbshipit-source-id: 9cbd05c5ca5e00227be69441b9d7d8a502690246 --- .../Animated/animations/TimingAnimation.js | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/react-native/Libraries/Animated/animations/TimingAnimation.js b/packages/react-native/Libraries/Animated/animations/TimingAnimation.js index f287c0dca75..2f6cba12947 100644 --- a/packages/react-native/Libraries/Animated/animations/TimingAnimation.js +++ b/packages/react-native/Libraries/Animated/animations/TimingAnimation.js @@ -113,25 +113,26 @@ export default class TimingAnimation extends Animation { this._fromValue = fromValue; this._onUpdate = onUpdate; - const start = () => { - if (!this._useNativeDriver && animatedValue.__isNative === true) { - throw new Error( - 'Attempting to run JS driven animation on animated node ' + - 'that has been moved to "native" earlier by starting an ' + - 'animation with `useNativeDriver: true`', - ); - } + if (!this._useNativeDriver && animatedValue.__isNative === true) { + throw new Error( + 'Attempting to run JS driven animation on animated node ' + + 'that has been moved to "native" earlier by starting an ' + + 'animation with `useNativeDriver: true`', + ); + } - // Animations that sometimes have 0 duration and sometimes do not - // still need to use the native driver when duration is 0 so as to - // not cause intermixed JS and native animations. - if (this._duration === 0 && !this._useNativeDriver) { - this._onUpdate(this._toValue); - this.__debouncedOnEnd({finished: true}); + const start = () => { + this._startTime = Date.now(); + + if (this._useNativeDriver) { + this.__startNativeAnimation(animatedValue); } else { - this._startTime = Date.now(); - if (this._useNativeDriver) { - this.__startNativeAnimation(animatedValue); + // Animations that sometimes have 0 duration and sometimes do not + // still need to use the native driver when duration is 0 so as to + // not cause intermixed JS and native animations. + if (this._duration === 0) { + this._onUpdate(this._toValue); + this.__debouncedOnEnd({finished: true}); } else { this._animationFrame = requestAnimationFrame( // $FlowFixMe[method-unbinding] added when improving typing for this parameters