Skip to content

Animated: Trigger Callback on Stopping Native Loops #52274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type CompositeAnimation = {
start: (callback?: ?EndCallback, isLooping?: boolean) => void,
stop: () => void,
reset: () => void,
_startNativeLoop: (iterations?: number) => void,
_startNativeLoop: (iterations: number, callback: ?EndCallback) => void,
_isUsingNativeDriver: () => boolean,
...
};
Expand Down Expand Up @@ -192,9 +192,9 @@ const springImpl = function (
value.resetAnimation();
},

_startNativeLoop: function (iterations?: number): void {
_startNativeLoop(iterations: number, callback: ?EndCallback): void {
const singleConfig = {...config, iterations};
start(value, singleConfig);
start(value, singleConfig, callback);
},

_isUsingNativeDriver: function (): boolean {
Expand Down Expand Up @@ -246,9 +246,9 @@ const timingImpl = function (
value.resetAnimation();
},

_startNativeLoop: function (iterations?: number): void {
_startNativeLoop(iterations: number, callback: ?EndCallback): void {
const singleConfig = {...config, iterations};
start(value, singleConfig);
start(value, singleConfig, callback);
},

_isUsingNativeDriver: function (): boolean {
Expand Down Expand Up @@ -288,9 +288,9 @@ const decayImpl = function (
value.resetAnimation();
},

_startNativeLoop: function (iterations?: number): void {
_startNativeLoop(iterations: number, callback: ?EndCallback): void {
const singleConfig = {...config, iterations};
start(value, singleConfig);
start(value, singleConfig, callback);
},

_isUsingNativeDriver: function (): boolean {
Expand Down Expand Up @@ -484,7 +484,7 @@ const loopImpl = function (
callback && callback({finished: true});
} else {
if (animation._isUsingNativeDriver()) {
animation._startNativeLoop(iterations);
animation._startNativeLoop(iterations, callback);
} else {
restart(); // Start looping recursively on the js thread
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Animated/AnimatedMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type CompositeAnimation = {
start: (callback?: ?EndCallback) => void,
stop: () => void,
reset: () => void,
_startNativeLoop: (iterations?: number) => void,
_startNativeLoop: (iterations: number, callback: ?EndCallback) => void,
_isUsingNativeDriver: () => boolean,
...
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,31 @@ describe('Animated', () => {
expect(animation.reset).toHaveBeenCalledTimes(1);
expect(cb).toBeCalledWith({finished: false});
});

it('stops looping native animations', () => {
const value = new Animated.Value(0);
const animation = Animated.timing(value, {
toValue: 1,
useNativeDriver: true,
});

jest.spyOn(animation, '_startNativeLoop');
jest.spyOn(animation, 'stop');

const callback = jest.fn();

const loop = Animated.loop(animation);
loop.start(callback);

expect(animation._startNativeLoop).toBeCalledTimes(1);
expect(animation.stop).not.toBeCalled();
expect(callback).not.toBeCalled();

loop.stop();

expect(animation.stop).toBeCalled();
expect(callback).toBeCalledWith({finished: false});
});
});

it('does not reset animation in a loop if resetBeforeIteration is false', () => {
Expand Down
Loading