From ded7119c9a8da545ca29ddb8436942183e286f2c Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Sat, 27 Apr 2019 10:16:12 -0700 Subject: [PATCH] default isInteraction to !useNativeDriver Summary: [General] [Changed] - If `isInteraction` is not specified in the config, it would always default to `true` which would block interactions like VirtualizedList updates. This is generally not what you want with useNativeDriver since the animation won't be interrupted by JS. If something does end up interfering with an animation and causes frame drops, `isInteraction` can be set manually. Reviewed By: yungsters Differential Revision: D14988087 fbshipit-source-id: 791b5cc327ffef6d2720c647a228cf3134a27cda --- .../Animated/src/animations/DecayAnimation.js | 8 ++-- .../src/animations/SpringAnimation.js | 43 +++++++------------ .../src/animations/TimingAnimation.js | 11 +++-- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/Libraries/Animated/src/animations/DecayAnimation.js b/Libraries/Animated/src/animations/DecayAnimation.js index 0e09b6144..a79f9c3ed 100644 --- a/Libraries/Animated/src/animations/DecayAnimation.js +++ b/Libraries/Animated/src/animations/DecayAnimation.js @@ -38,13 +38,11 @@ class DecayAnimation extends Animation { constructor(config: DecayAnimationConfigSingle) { super(); - this._deceleration = - config.deceleration !== undefined ? config.deceleration : 0.998; + this._deceleration = config.deceleration ?? 0.998; this._velocity = config.velocity; this._useNativeDriver = shouldUseNativeDriver(config); - this.__isInteraction = - config.isInteraction !== undefined ? config.isInteraction : true; - this.__iterations = config.iterations !== undefined ? config.iterations : 1; + this.__isInteraction = config.isInteraction ?? !this._useNativeDriver; + this.__iterations = config.iterations ?? 1; } __getNativeAnimationConfig() { diff --git a/Libraries/Animated/src/animations/SpringAnimation.js b/Libraries/Animated/src/animations/SpringAnimation.js index 497e72a2e..a7ffac480 100644 --- a/Libraries/Animated/src/animations/SpringAnimation.js +++ b/Libraries/Animated/src/animations/SpringAnimation.js @@ -51,13 +51,6 @@ export type SpringAnimationConfigSingle = AnimationConfig & { delay?: number, }; -function withDefault(value: ?T, defaultValue: T): T { - if (value === undefined || value === null) { - return defaultValue; - } - return value; -} - class SpringAnimation extends Animation { _overshootClamping: boolean; _restDisplacementThreshold: number; @@ -83,20 +76,16 @@ class SpringAnimation extends Animation { constructor(config: SpringAnimationConfigSingle) { super(); - this._overshootClamping = withDefault(config.overshootClamping, false); - this._restDisplacementThreshold = withDefault( - config.restDisplacementThreshold, - 0.001, - ); - this._restSpeedThreshold = withDefault(config.restSpeedThreshold, 0.001); - this._initialVelocity = withDefault(config.velocity, 0); - this._lastVelocity = withDefault(config.velocity, 0); + this._overshootClamping = config.overshootClamping ?? false; + this._restDisplacementThreshold = config.restDisplacementThreshold ?? 0.001; + this._restSpeedThreshold = config.restSpeedThreshold ?? 0.001; + this._initialVelocity = config.velocity ?? 0; + this._lastVelocity = config.velocity ?? 0; this._toValue = config.toValue; - this._delay = withDefault(config.delay, 0); + this._delay = config.delay ?? 0; this._useNativeDriver = shouldUseNativeDriver(config); - this.__isInteraction = - config.isInteraction !== undefined ? config.isInteraction : true; - this.__iterations = config.iterations !== undefined ? config.iterations : 1; + this.__isInteraction = config.isInteraction ?? !this._useNativeDriver; + this.__iterations = config.iterations ?? 1; if ( config.stiffness !== undefined || @@ -110,9 +99,9 @@ class SpringAnimation extends Animation { config.friction === undefined, 'You can define one of bounciness/speed, tension/friction, or stiffness/damping/mass, but not more than one', ); - this._stiffness = withDefault(config.stiffness, 100); - this._damping = withDefault(config.damping, 10); - this._mass = withDefault(config.mass, 1); + this._stiffness = config.stiffness ?? 100; + this._damping = config.damping ?? 10; + this._mass = config.mass ?? 1; } else if (config.bounciness !== undefined || config.speed !== undefined) { // Convert the origami bounciness/speed values to stiffness/damping // We assume mass is 1. @@ -125,8 +114,8 @@ class SpringAnimation extends Animation { 'You can define one of bounciness/speed, tension/friction, or stiffness/damping/mass, but not more than one', ); const springConfig = SpringConfig.fromBouncinessAndSpeed( - withDefault(config.bounciness, 8), - withDefault(config.speed, 12), + config.bounciness ?? 8, + config.speed ?? 12, ); this._stiffness = springConfig.stiffness; this._damping = springConfig.damping; @@ -135,8 +124,8 @@ class SpringAnimation extends Animation { // Convert the origami tension/friction values to stiffness/damping // We assume mass is 1. const springConfig = SpringConfig.fromOrigamiTensionAndFriction( - withDefault(config.tension, 40), - withDefault(config.friction, 7), + config.tension ?? 40, + config.friction ?? 7, ); this._stiffness = springConfig.stiffness; this._damping = springConfig.damping; @@ -157,7 +146,7 @@ class SpringAnimation extends Animation { stiffness: this._stiffness, damping: this._damping, mass: this._mass, - initialVelocity: withDefault(this._initialVelocity, this._lastVelocity), + initialVelocity: this._initialVelocity ?? this._lastVelocity, toValue: this._toValue, iterations: this.__iterations, }; diff --git a/Libraries/Animated/src/animations/TimingAnimation.js b/Libraries/Animated/src/animations/TimingAnimation.js index 61ad74e9e..a9569f01b 100644 --- a/Libraries/Animated/src/animations/TimingAnimation.js +++ b/Libraries/Animated/src/animations/TimingAnimation.js @@ -55,13 +55,12 @@ class TimingAnimation extends Animation { constructor(config: TimingAnimationConfigSingle) { super(); this._toValue = config.toValue; - this._easing = config.easing !== undefined ? config.easing : easeInOut(); - this._duration = config.duration !== undefined ? config.duration : 500; - this._delay = config.delay !== undefined ? config.delay : 0; - this.__iterations = config.iterations !== undefined ? config.iterations : 1; - this.__isInteraction = - config.isInteraction !== undefined ? config.isInteraction : true; + this._easing = config.easing ?? easeInOut(); + this._duration = config.duration ?? 500; + this._delay = config.delay ?? 0; + this.__iterations = config.iterations ?? 1; this._useNativeDriver = shouldUseNativeDriver(config); + this.__isInteraction = config.isInteraction ?? !this._useNativeDriver; } __getNativeAnimationConfig(): any {