Skip to content

Commit 0e4af15

Browse files
Fix Animated.sequence reset after the sequence finishes (#59916)
sequenceImpl rewound its current index to 0 as soon as the last animation finished, so a later reset() only reset the first animation (its filter is idx <= current) and every later value kept its end value. Leave the index on the last animation on completion and mark the sequence finished, then rewind at the next start() when that flag is set, which keeps restarting without reset working even if the animations array changed in between. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6125a5d commit 0e4af15

2 files changed

Lines changed: 103 additions & 5 deletions

File tree

‎packages/react-native/Libraries/Animated/AnimatedImplementation.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,23 +348,28 @@ const sequenceImpl = function (
348348
animations: Array<CompositeAnimation>,
349349
): CompositeAnimation {
350350
let current = 0;
351+
let finished = false;
351352
return {
352353
start: function (callback?: ?EndCallback, isLooping?: boolean) {
354+
if (finished) {
355+
// if the start is called, even without a reset, it should start from the beginning
356+
current = 0;
357+
finished = false;
358+
}
359+
353360
const onComplete = function (result: EndResult) {
354361
if (!result.finished) {
355362
callback && callback(result);
356363
return;
357364
}
358365

359-
current++;
360-
361-
if (current === animations.length) {
362-
// if the start is called, even without a reset, it should start from the beginning
363-
current = 0;
366+
if (current === animations.length - 1) {
367+
finished = true;
364368
callback && callback(result);
365369
return;
366370
}
367371

372+
current++;
368373
animations[current].start(onComplete, isLooping);
369374
};
370375

@@ -388,6 +393,7 @@ const sequenceImpl = function (
388393
}
389394
});
390395
current = 0;
396+
finished = false;
391397
},
392398

393399
_startNativeLoop: function () {

‎packages/react-native/Libraries/Animated/__tests__/Animated-test.js‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,73 @@ describe('Animated', () => {
351351
expect(cb).toBeCalledWith({finished: true});
352352
});
353353

354+
it('resets every value after a sequence finishes', () => {
355+
const first = new Animated.Value(0);
356+
const second = new Animated.Value(10);
357+
const sequence = Animated.sequence([
358+
Animated.timing(first, {
359+
toValue: 1,
360+
duration: 0,
361+
useNativeDriver: false,
362+
}),
363+
Animated.timing(second, {
364+
toValue: 20,
365+
duration: 0,
366+
useNativeDriver: false,
367+
}),
368+
]);
369+
const callback = jest.fn();
370+
371+
sequence.start(callback);
372+
expect(callback).toHaveBeenCalledWith({finished: true});
373+
expect(first.__getValue()).toBe(1);
374+
expect(second.__getValue()).toBe(20);
375+
376+
sequence.reset();
377+
378+
expect(first.__getValue()).toBe(0);
379+
expect(second.__getValue()).toBe(10);
380+
});
381+
382+
it('restarts from the first animation after a finished sequence grows', () => {
383+
const anim1 = {start: jest.fn()};
384+
const anim2 = {start: jest.fn()};
385+
const anim3 = {start: jest.fn()};
386+
const animations: Array<$FlowFixMe> = [anim1, anim2];
387+
const sequence = Animated.sequence(animations);
388+
389+
sequence.start(jest.fn());
390+
anim1.start.mock.calls[0][0]({finished: true});
391+
anim2.start.mock.calls[0][0]({finished: true});
392+
393+
animations.push(anim3);
394+
sequence.start(jest.fn());
395+
396+
expect(anim1.start).toHaveBeenCalledTimes(2);
397+
expect(anim3.start).not.toBeCalled();
398+
});
399+
400+
it('does not stop or reset an animation added after the sequence finished', () => {
401+
const anim1 = {start: jest.fn(), stop: jest.fn(), reset: jest.fn()};
402+
const anim2 = {start: jest.fn(), stop: jest.fn(), reset: jest.fn()};
403+
const anim3 = {start: jest.fn(), stop: jest.fn(), reset: jest.fn()};
404+
const animations: Array<$FlowFixMe> = [anim1, anim2];
405+
const sequence = Animated.sequence(animations);
406+
407+
sequence.start(jest.fn());
408+
anim1.start.mock.calls[0][0]({finished: true});
409+
anim2.start.mock.calls[0][0]({finished: true});
410+
411+
animations.push(anim3);
412+
sequence.stop();
413+
sequence.reset();
414+
415+
expect(anim3.stop).not.toBeCalled();
416+
expect(anim3.reset).not.toBeCalled();
417+
expect(anim1.reset).toBeCalled();
418+
expect(anim2.reset).toBeCalled();
419+
});
420+
354421
it('supports interrupting sequence', () => {
355422
const anim1 = {start: jest.fn()};
356423
const anim2 = {start: jest.fn()};
@@ -697,6 +764,31 @@ describe('Animated', () => {
697764
expect(anim1.start).toHaveBeenCalledTimes(2);
698765
});
699766

767+
it('resets every sequence value before the next loop iteration', () => {
768+
const first = new Animated.Value(0);
769+
const second = new Animated.Value(10);
770+
const secondValues: Array<number> = [];
771+
first.addListener(() => secondValues.push(second.__getValue()));
772+
const sequence = Animated.sequence([
773+
Animated.timing(first, {
774+
toValue: 1,
775+
duration: 0,
776+
useNativeDriver: false,
777+
}),
778+
Animated.timing(second, {
779+
toValue: 20,
780+
duration: 0,
781+
useNativeDriver: false,
782+
}),
783+
]);
784+
const callback = jest.fn();
785+
786+
Animated.loop(sequence, {iterations: 2}).start(callback);
787+
788+
expect(callback).toHaveBeenCalledWith({finished: true});
789+
expect(secondValues).toEqual([10, 10]);
790+
});
791+
700792
describe('Animated Parallel', () => {
701793
it('works with an empty parallel', () => {
702794
const cb = jest.fn();

0 commit comments

Comments
 (0)