From fea2db42fdbbb4e9cf6420f77048c7dab47b4ef0 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 22 Jul 2015 17:20:27 -0700 Subject: [PATCH] [Animated] Send a final update with toValue for spring Summary: Animated.spring is not guarantee to stabilize at exactly toValue (determined by restDisplacementThreshold). It is a bit annoying that the last value is not toValue, it makes the logs harder to read and also prevents you from writing code like value === toValue. Instead you need to track it down somewhere else. --- Libraries/Animation/Animated/Animated.js | 6 ++++++ .../Animation/Animated/__tests__/Animated-test.js | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Libraries/Animation/Animated/Animated.js b/Libraries/Animation/Animated/Animated.js index f99913f50..8f3f38398 100644 --- a/Libraries/Animation/Animated/Animated.js +++ b/Libraries/Animation/Animated/Animated.js @@ -479,7 +479,13 @@ class SpringAnimation extends Animation { if (this._tension !== 0) { isDisplacement = Math.abs(this._toValue - position) <= this._restDisplacementThreshold; } + if (isOvershooting || (isVelocity && isDisplacement)) { + if (this._tension !== 0) { + // Ensure that we end up with a round value + this._onUpdate(this._toValue); + } + this.__debouncedOnEnd({finished: true}); return; } diff --git a/Libraries/Animation/Animated/__tests__/Animated-test.js b/Libraries/Animation/Animated/__tests__/Animated-test.js index cad752ff0..d27ff920c 100644 --- a/Libraries/Animation/Animated/__tests__/Animated-test.js +++ b/Libraries/Animation/Animated/__tests__/Animated-test.js @@ -127,6 +127,19 @@ describe('Animated', () => { Animated.spring(anim, {toValue: 0, velocity: 0}).start(callback); expect(callback).toBeCalled(); }); + + it('send toValue when a spring stops', () => { + var anim = new Animated.Value(0); + var listener = jest.genMockFunction(); + anim.addListener(listener); + Animated.spring(anim, {toValue: 15}).start(); + jest.runAllTimers(); + var lastValue = listener.mock.calls[listener.mock.calls.length - 2][0].value; + expect(lastValue).not.toBe(15); + expect(lastValue).toBeCloseTo(15); + expect(anim.__getValue()).toBe(15); + }); + });