Skip to content

Commit 53b87dc

Browse files
sokkifacebook-github-bot
authored andcommitted
add new config resetBeforeIteration for Animated.loop (facebook#20561)
Summary: Fixes facebook#20560 Pull Request resolved: facebook#20561 Differential Revision: D14103437 Pulled By: cpojer fbshipit-source-id: 35f2b3426304248e1a28f6a88812f07414618ea2
1 parent 65cfb9a commit 53b87dc

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

Libraries/Animated/src/AnimatedImplementation.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,14 @@ const stagger = function(
417417
);
418418
};
419419

420-
type LoopAnimationConfig = {iterations: number};
420+
type LoopAnimationConfig = {
421+
iterations: number,
422+
resetBeforeIteration?: boolean,
423+
};
421424

422425
const loop = function(
423426
animation: CompositeAnimation,
424-
{iterations = -1}: LoopAnimationConfig = {},
427+
{iterations = -1, resetBeforeIteration = true}: LoopAnimationConfig = {},
425428
): CompositeAnimation {
426429
let isFinished = false;
427430
let iterationsSoFar = 0;
@@ -436,7 +439,7 @@ const loop = function(
436439
callback && callback(result);
437440
} else {
438441
iterationsSoFar++;
439-
animation.reset();
442+
resetBeforeIteration && animation.reset();
440443
animation.start(restart);
441444
}
442445
};

Libraries/Animated/src/__tests__/Animated-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,37 @@ describe('Animated tests', () => {
445445
});
446446
});
447447

448+
it('does not reset animation in a loop if resetBeforeIteration is false', () => {
449+
const animation = {
450+
start: jest.fn(),
451+
reset: jest.fn(),
452+
_isUsingNativeDriver: () => false,
453+
};
454+
const cb = jest.fn();
455+
456+
const loop = Animated.loop(animation, {resetBeforeIteration: false});
457+
458+
expect(animation.start).not.toBeCalled();
459+
460+
loop.start(cb);
461+
462+
expect(animation.start).toBeCalled();
463+
expect(animation.reset).not.toBeCalled();
464+
expect(cb).not.toBeCalled();
465+
466+
animation.start.mock.calls[0][0]({finished: true}); // End of loop 1
467+
expect(animation.reset).not.toBeCalled();
468+
expect(cb).not.toBeCalled();
469+
470+
animation.start.mock.calls[0][0]({finished: true}); // End of loop 2
471+
expect(animation.reset).not.toBeCalled();
472+
expect(cb).not.toBeCalled();
473+
474+
animation.start.mock.calls[0][0]({finished: true}); // End of loop 3
475+
expect(animation.reset).not.toBeCalled();
476+
expect(cb).not.toBeCalled();
477+
});
478+
448479
describe('Animated Parallel', () => {
449480
it('works with an empty parallel', () => {
450481
const cb = jest.fn();

0 commit comments

Comments
 (0)