Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): each animation's end recheck in the animator is its own (#9325)

Christopher Chedeau5a4e8fa84110
diff --git a/.changeset/animator-retire-look-per-animation.md b/.changeset/animator-retire-look-per-animation.md
new file mode 100644
index 0000000000..c9b5ca0706
--- /dev/null
+++ b/.changeset/animator-retire-look-per-animation.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[chore] Internal: the touch presentations' animator keeps each animation's frame-callback recheck of its curve's end its own, so a recheck still waiting for a replaced animation no longer stands in for the next animation's, which then fell back to its timer. Not exported from the package entry point.
+
+@vjeux
diff --git a/packages/core/src/motion/animator.test.ts b/packages/core/src/motion/animator.test.ts
index 126d7e32c0..89aefce585 100644
--- a/packages/core/src/motion/animator.test.ts
+++ b/packages/core/src/motion/animator.test.ts
@@ -552,6 +552,50 @@ describe('animate: keyframes on the compositor', () => {
expect(animation.cancelled).toBe(true);
});
+ it('a retarget while a look at the old curve\u2019s end waits for its frame still looks at the new curve\u2019s end in a frame', async () => {
+ // Each animation's frame look is its own: one still waiting for its frame
+ // for a replaced animation does not stand in for the new animation's.
+ const {animator} = setup();
+ animator.animate({x: 100}, {spring: springs.settle});
+ const [first] = live('translate');
+ await commitFrame(clock);
+ // The engine dates each animation per frame, at `frameTime`.
+ let frameTime = clock;
+ const datedPerFrame = (animation: Recorded) => {
+ Object.defineProperty(animation, 'currentTime', {
+ configurable: true,
+ get: () => frameTime - (animation.startTime as number),
+ });
+ Object.defineProperty(animation, 'effect', {
+ configurable: true,
+ value: {getTiming: () => ({duration: animation.options.duration})},
+ });
+ };
+ datedPerFrame(first);
+ const firstCurve = Number(first.options.duration) - END_HOLD_MS;
+ // Inside the last frame before its end, with no frame drawn: its look
+ // waits for the next frame.
+ clock += firstCurve - 10;
+ await vi.advanceTimersByTimeAsync(firstCurve - 10);
+ expect(frameQueue.length).toBeGreaterThan(0);
+ // Retargeted before that frame comes; the page then draws no frame until
+ // the new curve is inside its own last frame.
+ animator.animate({x: 200}, {spring: springs.settle});
+ const second = live('translate').at(-1) as Recorded;
+ datedPerFrame(second);
+ const secondCurve = Number(second.options.duration) - END_HOLD_MS;
+ const secondEnd = (second.startTime as number) + secondCurve;
+ const wait = secondEnd - 10 - clock;
+ clock += wait;
+ await vi.advanceTimersByTimeAsync(wait);
+ expect(second.cancelled).toBe(false);
+ // The next frame reaches the new curve's end: it retires there, before
+ // that frame paints, with no timer run in between.
+ frameTime = secondEnd + 1;
+ await commitFrame(clock);
+ expect(second.cancelled).toBe(true);
+ });
+
it('never retires an animation a sampler has paused', async () => {
const {animator} = setup();
animator.animate({x: 120}, {spring: springs.quickChange});
diff --git a/packages/core/src/motion/animator.ts b/packages/core/src/motion/animator.ts
index eafb3a7b42..715e8be267 100644
--- a/packages/core/src/motion/animator.ts
+++ b/packages/core/src/motion/animator.ts
@@ -961,8 +961,12 @@ export function createAnimator(
const running = new Map<Channel, Animation>();
/** Drops a finished, still-filling animation once the main thread runs. */
const retireTimers = new Map<Channel, ReturnType<typeof setTimeout>>();
- /** Channels whose retire looks again at the next frame (scheduleRetire). */
- const retireFrames = new Set<Channel>();
+ /**
+ * Animations whose retire looks again at the next frame (scheduleRetire):
+ * each its own, so one still waiting for a replaced animation does not
+ * stand in for the next.
+ */
+ const retireFrames = new Set<Animation>();
/** The timer that starts those looks a frame before a curve's end. */
const retireLooks = new Map<Channel, ReturnType<typeof setTimeout>>();
const touched = new Set<Channel>();
@@ -1464,12 +1468,12 @@ export function createAnimator(
// past its curve. The timers stay, for a page that draws no frames (a
// hidden tab).
const lookAtFrame = () => {
- if (requestFrame == null || retireFrames.has(channel)) {
+ if (requestFrame == null || retireFrames.has(animation)) {
return;
}
- retireFrames.add(channel);
+ retireFrames.add(animation);
requestFrame(() => {
- retireFrames.delete(channel);
+ retireFrames.delete(animation);
retire(true);
});
};