Skip to content

Commit

Permalink
Deduct offset from getValue result when detaching
Browse files Browse the repository at this point in the history
Summary:
The NativeAnimated `getValue` API returns `value + offset`:
Android:
iOS: https://github.com/facebook/react-native/blob/main/Libraries/NativeAnimation/Nodes/RCTValueAnimatedNode.m#L44
Android: https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.java#L36

When we store the value after detaching the NativeAnimated node, it stores the result of the NativeAnimated `getValue` call to the `_value` property, so if we call `__getValue` at some later point on the `AnimatedValue`, we will count the offset twice.

This change deducts the offset value from the result returned from `getValue` when storing the latest value.

Changelog:
[General][Fixed] - AnimatedValue.__detach should store getValue result with offset deducted

Reviewed By: yungsters

Differential Revision: D32987003

fbshipit-source-id: 488d1fe512f886c7a9de1e5a4de8f19441ebd81e
  • Loading branch information
rozele authored and facebook-github-bot committed Dec 10, 2021
1 parent c034b7e commit fe53cae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Libraries/Animated/__tests__/AnimatedNative-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ describe('Native Animated', () => {
expect(opacity.__getValue()).toBe(1);
});

it('should deduct offset when saving value on unmount', () => {
NativeAnimatedModule.getValue = jest.fn((tag, saveCallback) => {
// Assume current raw value of value node is 0.5, the NativeAnimated
// getValue API returns the sum of raw value and offset, so return 1.
saveCallback(1);
});
const opacity = new Animated.Value(0);
opacity.setOffset(0.5);
opacity.__makeNative();

const root = TestRenderer.create(<Animated.View style={{opacity}} />);
const tag = opacity.__getNativeTag();

root.unmount();

expect(NativeAnimatedModule.getValue).toBeCalledWith(
tag,
expect.any(Function),
);
expect(opacity.__getValue()).toBe(1);
});

it('should extract offset', () => {
const opacity = new Animated.Value(0);
opacity.__makeNative();
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Animated/nodes/AnimatedValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AnimatedValue extends AnimatedWithChildren {
__detach() {
if (this.__isNative) {
NativeAnimatedAPI.getValue(this.__getNativeTag(), value => {
this._value = value;
this._value = value - this._offset;
});
}
this.stopAnimation();
Expand Down

0 comments on commit fe53cae

Please sign in to comment.