Skip to content

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

TypeScript

fix(interaction-harness): a run held while its resume is pending keeps its place at any playback rate (#9109)

Christopher Chedeau47dd87e3d540
diff --git a/internal/interaction-harness/src/curves.ts b/internal/interaction-harness/src/curves.ts
index 738bea2d6e..18ef2a80c3 100644
--- a/internal/interaction-harness/src/curves.ts
+++ b/internal/interaction-harness/src/curves.ts
@@ -416,11 +416,14 @@ export async function holdAnimations(
);
}
// Each animation's start on the document timeline, read before the pause
- // clears it (a pending play has no start time yet: it starts now).
+ // clears it. A pending play has no start time yet: it starts now, as far
+ // back as its current time reaches through its playback rate (at rate 2,
+ // a run covers its curve in half the timeline), as seeking reads it.
const starts = all.map(animation =>
animation.startTime != null
? Number(animation.startTime)
- : now - Number(animation.currentTime ?? 0),
+ : now -
+ Number(animation.currentTime ?? 0) / (animation.playbackRate || 1),
);
const held = all.map(animation => {
animation.pause();
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index 6cd113b750..4ab55078bc 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -2171,6 +2171,88 @@ test.describe('holding', () => {
await releaseAnimations(page);
});
+ test('a run held while its resume is still pending keeps its place through its playback rate: a seek to the hold leaves it where it was held', async ({
+ page,
+ }) => {
+ await fixture(page);
+ // #box runs at rate 1; a second run at rate 2 was paused 400 ms into its
+ // curve and is resumed by the hold's own first read of the timeline, so
+ // its resume is still pending (no start time yet) when the hold takes it.
+ // It covers its 400 ms of curve in 200 ms of timeline. Both run long
+ // enough for the hold to reach them on a loaded runner, where a hold can
+ // read the page seconds after the runs were made.
+ await page.evaluate(async () => {
+ const frames = [
+ {transform: 'translateX(0px)'},
+ {transform: 'translateX(300px)'},
+ ];
+ document
+ .getElementById('box')
+ ?.animate(frames, {duration: 60_000, easing: 'linear'});
+ const other = document.createElement('div');
+ other.id = 'faster';
+ other.style.cssText =
+ 'position:absolute;left:0;top:300px;width:40px;height:40px;background:#c63';
+ document.body.append(other);
+ const faster = other.animate(frames, {
+ duration: 60_000,
+ easing: 'linear',
+ });
+ faster.playbackRate = 2;
+ faster.pause();
+ faster.currentTime = 400;
+ await new Promise(resolve =>
+ requestAnimationFrame(() => requestAnimationFrame(resolve)),
+ );
+ // The hold reads the timeline's time once, just before it lists the
+ // page's animations: resumed there, the run is pending when listed. No
+ // other read of the timeline's time comes between (the animation
+ // recorder reads the clock and the animations, never the timeline).
+ // The read is caught on the prototype that defines it, which lives as
+ // long as the page: a property set on the timeline object itself is
+ // lost when the engine collects and recreates its wrapper.
+ let owner: object | null = Object.getPrototypeOf(document.timeline);
+ while (
+ owner != null &&
+ Object.getOwnPropertyDescriptor(owner, 'currentTime') == null
+ ) {
+ owner = Object.getPrototypeOf(owner);
+ }
+ const real =
+ owner == null
+ ? undefined
+ : Object.getOwnPropertyDescriptor(owner, 'currentTime');
+ if (owner == null || real?.get == null) {
+ throw new Error('no currentTime getter on the timeline');
+ }
+ const prototype = owner;
+ const read = real.get;
+ Object.defineProperty(prototype, 'currentTime', {
+ configurable: true,
+ get(this: AnimationTimeline) {
+ Object.defineProperty(prototype, 'currentTime', real);
+ faster.play();
+ return read.call(this);
+ },
+ });
+ });
+ const held = await holdAnimations(page);
+ expect(held.map(a => a.target)).toEqual(['div#box', 'div#faster']);
+ const [box, faster] = held;
+ expect(faster.heldAtMs).toBe(400);
+ // The moment of the hold, from #box's own place: a seek there leaves
+ // every held run where the hold read it.
+ await seekAnimations(page, (box.heldAtMs ?? 0) + box.offsetMs);
+ const at = await page.evaluate(() =>
+ ['box', 'faster'].map(id =>
+ Number(document.getElementById(id)?.getAnimations()[0]?.currentTime),
+ ),
+ );
+ expect(Math.abs(at[0] - (box.heldAtMs ?? 0))).toBeLessThanOrEqual(1);
+ expect(Math.abs(at[1] - 400)).toBeLessThanOrEqual(1);
+ await releaseAnimations(page);
+ });
+
test('a second hold adds to the held set, and releasing resumes both', async ({
page,
}) => {