Skip to content

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

TypeScript

fix(core): the animator rechecks a finished animation's end in frame callbacks from a frame before it (#9285)

Christopher Chedeau70cef4580a2e
diff --git a/.changeset/animator-retire-at-frame.md b/.changeset/animator-retire-at-frame.md
new file mode 100644
index 0000000000..662d20525c
--- /dev/null
+++ b/.changeset/animator-retire-at-frame.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[chore] Internal: the touch presentations' animator rechecks a finished animation's end in frame callbacks, starting a frame before it and looking again while up to two frames remain, instead of only on timers that a loaded page can run a frame or two late, so the frame that reaches the curve's end drops the animation before it paints, unless the timer that starts those rechecks itself runs more than a frame late. The timers remain for a page that draws no frames. 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 01846d89af..4db132b502 100644
--- a/packages/core/src/motion/animator.test.ts
+++ b/packages/core/src/motion/animator.test.ts
@@ -469,7 +469,7 @@ describe('animate: keyframes on the compositor', () => {
);
});
- it('looks for its end again on the next frame when the engine dates animations per frame (WebKit), not in a spin', async () => {
+ it('looks for its end again on the next frame when the engine dates animations per frame, not in a spin, and retires in that frame', async () => {
// WebKit moves an animation's currentTime once per frame. A retire that
// looks within a frame reads the frame's time, a fraction of a ms short of
// the end, and a timer set to that fraction fires again before anything
@@ -502,14 +502,51 @@ describe('animate: keyframes on the compositor', () => {
await vi.advanceTimersByTimeAsync(1);
}
expect(timers.mock.calls.length).toBeLessThanOrEqual(2);
- // The next frame: the engine's time passes the end, and it retires.
+ // The next frame: the engine's time passes the end, and it retires in
+ // that frame, before it paints, with no timer run in between.
frameTime = start + curve + 16;
clock += 1;
- await vi.advanceTimersByTimeAsync(17);
+ await commitFrame(clock);
expect(animation.cancelled).toBe(true);
+ await vi.advanceTimersByTimeAsync(17);
await expect(handle.finished).resolves.toBe(true);
});
+ it('keeps looking while the frame it looked in was dated short of the end, and retires in the frame that reaches it, before a late end timer runs', async () => {
+ const {animator} = setup();
+ animator.animate({x: 100}, {spring: springs.settle});
+ const [animation] = live('translate');
+ await commitFrame(clock);
+ const start = animation.startTime as number;
+ const curve = Number(animation.options.duration) - END_HOLD_MS;
+ const end = start + curve;
+ // The engine dates each animation per frame, at `frameTime`.
+ let frameTime = start;
+ Object.defineProperty(animation, 'currentTime', {
+ configurable: true,
+ get: () => frameTime - start,
+ });
+ Object.defineProperty(animation, 'effect', {
+ configurable: true,
+ value: {getTiming: () => ({duration: animation.options.duration})},
+ });
+ // Inside the last frame before the end: the timer that starts the frame
+ // looks has run, the end timer has not.
+ const toLook = end - 16 - clock;
+ clock += toLook;
+ await vi.advanceTimersByTimeAsync(toLook);
+ // The frame the look runs in is dated 20 ms short of the end, before
+ // that timer was due.
+ frameTime = end - 20;
+ await commitFrame(clock);
+ expect(animation.cancelled).toBe(false);
+ // The next frame reaches the end while the end timer, late, has still
+ // not run: the frame retires it before it paints.
+ frameTime = end + 1;
+ await commitFrame(clock);
+ expect(animation.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 a2335b3975..dcd5676655 100644
--- a/packages/core/src/motion/animator.ts
+++ b/packages/core/src/motion/animator.ts
@@ -957,6 +957,10 @@ 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>();
+ /** 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>();
/**
* The stylesheet's opacity a Reduce Motion dip borrowed because no call
@@ -1179,6 +1183,8 @@ export function createAnimator(
running.delete(channel);
clearTimeout(retireTimers.get(channel));
retireTimers.delete(channel);
+ clearTimeout(retireLooks.get(channel));
+ retireLooks.delete(channel);
// An element's inline style takes the value on screen below, so its old
// animation can go first. A pseudo-element has none: its old animation
// keeps holding the value until the new one exists (same task, no frame in
@@ -1369,26 +1375,61 @@ export function createAnimator(
end: number,
) => {
clearTimeout(retireTimers.get(channel));
- const retire = () => {
- retireTimers.delete(channel);
+ retireTimers.delete(channel);
+ clearTimeout(retireLooks.get(channel));
+ retireLooks.delete(channel);
+ // Looks for the end in the next frame, before it paints: the frame whose
+ // time reaches the curve's end retires it there, where a timer could land
+ // between two frames under load and leave it on the element a frame or two
+ // past its curve. The timers stay, for a page that draws no frames (a
+ // hidden tab).
+ const lookAtFrame = () => {
+ if (requestFrame == null || retireFrames.has(channel)) {
+ return;
+ }
+ retireFrames.add(channel);
+ requestFrame(() => {
+ retireFrames.delete(channel);
+ retire(true);
+ });
+ };
+ // `atFrame`: a look from a frame callback, which leaves a pending timer
+ // where it is.
+ const retire = (atFrame = false) => {
+ if (!atFrame) {
+ retireTimers.delete(channel);
+ }
if (running.get(channel) !== animation) {
return;
}
// Paused or sought (a sampler holding it): retire when its own time
// reaches the curve's end, never from the wall clock.
const remaining = remainingCurve(animation);
+ if (atFrame && remaining == null) {
+ // Its time cannot be read: the timer decides.
+ return;
+ }
if (remaining != null && remaining > 0) {
- retireTimers.set(
- channel,
- setTimeout(
- retire,
- animation.playState === 'paused'
- ? RETIRE_RECHECK_MS
- : Math.max(remaining, END_RECHECK_MS),
- ),
- );
+ const paused = animation.playState === 'paused';
+ // A frame look keeps looking while up to two frames remain: the frame
+ // it ran in can be dated before the timer that started the looks was
+ // due, and the frame after it is the one that reaches the end.
+ if (!paused && remaining <= (atFrame ? 2 : 1) * END_RECHECK_MS) {
+ lookAtFrame();
+ }
+ if (!retireTimers.has(channel)) {
+ retireTimers.set(
+ channel,
+ setTimeout(
+ retire,
+ paused ? RETIRE_RECHECK_MS : Math.max(remaining, END_RECHECK_MS),
+ ),
+ );
+ }
return;
}
+ clearTimeout(retireTimers.get(channel));
+ retireTimers.delete(channel);
running.delete(channel);
// A pseudo-element holds its resting value, unless the stylesheet
// takes it back now (a borrowed dip that has ended).
@@ -1423,6 +1464,20 @@ export function createAnimator(
flushReleases();
};
retireTimers.set(channel, setTimeout(retire, Math.max(0, end - now())));
+ if (requestFrame != null) {
+ // Frames start looking a frame before the end, so the frame that
+ // reaches it retires it even when the timer above runs late.
+ retireLooks.set(
+ channel,
+ setTimeout(
+ () => {
+ retireLooks.delete(channel);
+ lookAtFrame();
+ },
+ Math.max(0, end - now() - END_RECHECK_MS),
+ ),
+ );
+ }
};
/** The element's own (stylesheet) opacity, read once when a dip starts. */
@@ -2969,6 +3024,11 @@ export function createAnimator(
clearTimeout(timer);
}
retireTimers.clear();
+ for (const timer of retireLooks.values()) {
+ clearTimeout(timer);
+ }
+ retireLooks.clear();
+ retireFrames.clear();
demote();
// demote() asks for a fresh will-change read of the node it
// demotes; a detached node gets none.
@@ -3018,6 +3078,11 @@ export function createAnimator(
clearTimeout(timer);
}
retireTimers.clear();
+ for (const timer of retireLooks.values()) {
+ clearTimeout(timer);
+ }
+ retireLooks.clear();
+ retireFrames.clear();
if (isOpacityBorrowed) {
// A running borrowed dip (or an exit by role returning to the
// stylesheet) stays in the model through any attach, so a pending
@@ -3622,6 +3687,11 @@ export function createAnimator(
clearTimeout(timer);
}
retireTimers.clear();
+ for (const timer of retireLooks.values()) {
+ clearTimeout(timer);
+ }
+ retireLooks.clear();
+ retireFrames.clear();
handBackBorrow();
demote();
cancelSheetWillChangeRead();