Skip to content

Commit 8ba771c

Browse files
rozelefacebook-github-bot
authored andcommitted
Fire AnimatedValue.stopAnimation callback with correct native value
Summary: AnimatedValue fires a callback with the current value (raw value + offset) after calling it's `stopAnimation` method. The `_value` field on the `AnimatedValue` node is not necessarily kept in sync with the NativeAnimated value node, so the value provided to the callback may be out of sync. This change checks if the `AnimatedValue` is a native node and passes the callback to the `NativeAnimatedAPI.getValue` callback, defaulting to the previous current JS node state if the node is not native. Changelog: [General][Fixed] - AnimatedValue.stopAnimation callback with correct value for NativeAnimated Reviewed By: yungsters Differential Revision: D32968572 fbshipit-source-id: b83f86eabe5456f762a15bc29cacb43f84513f6c
1 parent 1d4e7f6 commit 8ba771c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Libraries/Animated/__tests__/AnimatedNative-test.js

+24
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,30 @@ describe('Native Animated', () => {
928928
animation.stop();
929929
expect(NativeAnimatedModule.stopAnimation).toBeCalledWith(animationId);
930930
});
931+
932+
it('calls stopAnimation callback with native value', () => {
933+
NativeAnimatedModule.getValue = jest.fn((tag, saveCallback) => {
934+
saveCallback(1);
935+
});
936+
937+
const anim = new Animated.Value(0);
938+
Animated.timing(anim, {
939+
duration: 1000,
940+
useNativeDriver: true,
941+
}).start();
942+
943+
const tag = anim.__getNativeTag();
944+
945+
let currentValue = 0;
946+
anim.stopAnimation(value => (currentValue = value));
947+
948+
expect(NativeAnimatedModule.getValue).toBeCalledWith(
949+
tag,
950+
expect.any(Function),
951+
);
952+
953+
expect(currentValue).toEqual(1);
954+
});
931955
});
932956

933957
describe('Animated Components', () => {

Libraries/Animated/nodes/AnimatedValue.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,13 @@ class AnimatedValue extends AnimatedWithChildren {
186186
this.stopTracking();
187187
this._animation && this._animation.stop();
188188
this._animation = null;
189-
callback && callback(this.__getValue());
189+
if (callback) {
190+
if (this.__isNative) {
191+
NativeAnimatedAPI.getValue(this.__getNativeTag(), callback);
192+
} else {
193+
callback(this.__getValue());
194+
}
195+
}
190196
}
191197

192198
/**

0 commit comments

Comments
 (0)