Skip to content

Commit

Permalink
Fire AnimatedValue.stopAnimation callback with correct native value
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rozele authored and facebook-github-bot committed Dec 9, 2021
1 parent 1d4e7f6 commit 8ba771c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Libraries/Animated/__tests__/AnimatedNative-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,30 @@ describe('Native Animated', () => {
animation.stop();
expect(NativeAnimatedModule.stopAnimation).toBeCalledWith(animationId);
});

it('calls stopAnimation callback with native value', () => {
NativeAnimatedModule.getValue = jest.fn((tag, saveCallback) => {
saveCallback(1);
});

const anim = new Animated.Value(0);
Animated.timing(anim, {
duration: 1000,
useNativeDriver: true,
}).start();

const tag = anim.__getNativeTag();

let currentValue = 0;
anim.stopAnimation(value => (currentValue = value));

expect(NativeAnimatedModule.getValue).toBeCalledWith(
tag,
expect.any(Function),
);

expect(currentValue).toEqual(1);
});
});

describe('Animated Components', () => {
Expand Down
8 changes: 7 additions & 1 deletion Libraries/Animated/nodes/AnimatedValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@ class AnimatedValue extends AnimatedWithChildren {
this.stopTracking();
this._animation && this._animation.stop();
this._animation = null;
callback && callback(this.__getValue());
if (callback) {
if (this.__isNative) {
NativeAnimatedAPI.getValue(this.__getNativeTag(), callback);
} else {
callback(this.__getValue());
}
}
}

/**
Expand Down

0 comments on commit 8ba771c

Please sign in to comment.