Skip to content

Commit

Permalink
add new config resetBeforeIteration for Animated.loop (#20561)
Browse files Browse the repository at this point in the history
Summary:
Fixes #20560
Pull Request resolved: #20561

Differential Revision: D14103437

Pulled By: cpojer

fbshipit-source-id: 35f2b3426304248e1a28f6a88812f07414618ea2
  • Loading branch information
sokki authored and facebook-github-bot committed Feb 15, 2019
1 parent 65cfb9a commit 53b87dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Libraries/Animated/src/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,14 @@ const stagger = function(
);
};

type LoopAnimationConfig = {iterations: number};
type LoopAnimationConfig = {
iterations: number,
resetBeforeIteration?: boolean,
};

const loop = function(
animation: CompositeAnimation,
{iterations = -1}: LoopAnimationConfig = {},
{iterations = -1, resetBeforeIteration = true}: LoopAnimationConfig = {},
): CompositeAnimation {
let isFinished = false;
let iterationsSoFar = 0;
Expand All @@ -436,7 +439,7 @@ const loop = function(
callback && callback(result);
} else {
iterationsSoFar++;
animation.reset();
resetBeforeIteration && animation.reset();
animation.start(restart);
}
};
Expand Down
31 changes: 31 additions & 0 deletions Libraries/Animated/src/__tests__/Animated-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,37 @@ describe('Animated tests', () => {
});
});

it('does not reset animation in a loop if resetBeforeIteration is false', () => {
const animation = {
start: jest.fn(),
reset: jest.fn(),
_isUsingNativeDriver: () => false,
};
const cb = jest.fn();

const loop = Animated.loop(animation, {resetBeforeIteration: false});

expect(animation.start).not.toBeCalled();

loop.start(cb);

expect(animation.start).toBeCalled();
expect(animation.reset).not.toBeCalled();
expect(cb).not.toBeCalled();

animation.start.mock.calls[0][0]({finished: true}); // End of loop 1
expect(animation.reset).not.toBeCalled();
expect(cb).not.toBeCalled();

animation.start.mock.calls[0][0]({finished: true}); // End of loop 2
expect(animation.reset).not.toBeCalled();
expect(cb).not.toBeCalled();

animation.start.mock.calls[0][0]({finished: true}); // End of loop 3
expect(animation.reset).not.toBeCalled();
expect(cb).not.toBeCalled();
});

describe('Animated Parallel', () => {
it('works with an empty parallel', () => {
const cb = jest.fn();
Expand Down

0 comments on commit 53b87dc

Please sign in to comment.