fix(studio): a sealed press starts as soon as the frame answers (#8688)

* test(studio): a sealed press asks the frame at once and starts when it answers

* fix(studio): a sealed press starts as soon as the frame answers

* refactor(studio): the press's first poll in its own helper

* test(studio): a sealed keyframe read waits for the frame's values at the playhead
This commit is contained in:
Miguel Ángel
2026-11-24 10:05:01 +00:00
committed by GitHub
parent a8f64830a4
commit b9dddf3f4a
3 changed files with 52 additions and 2 deletions
@@ -41,6 +41,15 @@ type ShownStart = (
const MAX_SETTLED_WAIT_FRAMES = 60;
export { PRESS_WAIT_MAX_MS };
const pollFirst = (
channel: HTMLIFrameElement | null,
press: WaitingPressState,
poll: () => void,
) => {
if (channel) poll();
else press.frame = requestAnimationFrame(poll);
};
export function createPreviewGestureStarts(
opts: UseDomEditOverlayGesturesOptions,
moveActiveGesture: (e: React.PointerEvent<HTMLDivElement>) => void,
@@ -205,7 +214,11 @@ export function createPreviewGestureStarts(
opts.overlayRef.current,
opts.activeCompositionPathRef.current,
targets(),
).then((answer) => askedOf === frame && (overChannel = answer));
).then((answer) => {
if (askedOf !== frame) return;
overChannel = answer;
pollNow();
});
};
const channelStep = (): "wait" | "lost" | "run" | "drawn" => {
const overdue = performance.now() - since > PRESS_WAIT_MAX_MS;
@@ -244,6 +257,10 @@ export function createPreviewGestureStarts(
if (read === false) sayWhyNotStarted(step);
outline.replayEnded(replay(read) && channel ? opts.iframeRef.current : null);
};
const pollNow = () => {
cancelAnimationFrame(press.frame);
poll();
};
const replay = (read: ReturnType<typeof startsFrom>): boolean => {
const pointerDown = down as unknown as React.PointerEvent<HTMLElement>;
if (read === false || !start(pointerDown, at, true, read)) return false;
@@ -256,7 +273,7 @@ export function createPreviewGestureStarts(
if (step === "drawn" && channel && pressed)
opts.onBlockedMoveRef.current(pressed, unansweredMoveReason(channel));
};
press.frame = requestAnimationFrame(poll);
pollFirst(channel, press, poll);
return true;
};
@@ -756,6 +756,20 @@ describe("a press on a settled preview", () => {
expect(press.start.mock.calls[0]![3]).toEqual({ at: expect.any(Object) });
});
// fails without the fix: the press asked the frame only on the next animation frame, and started on the one after.
it("on an opaque frame asks at the press and starts once the frame answers, with no animation frame", async () => {
const raf = vi.spyOn(window, "requestAnimationFrame").mockImplementation(() => 0);
try {
const iframe = opaqueFrame();
const handle = (await queryPreviewElements(iframe, "#turned"))!.matches[0]!.handle;
const turned = document.getElementById("turned")!;
pressOn(iframe, { ...makeSelection("turned", turned), handle });
await vi.waitFor(() => expect(press.start).toHaveBeenCalledOnce());
} finally {
raf.mockRestore();
}
});
it("on an opaque frame waits for what the gesture starts from, read in one request", async () => {
const { port1 } = new MessageChannel();
const send = vi.spyOn(Object.getPrototypeOf(port1) as MessagePort, "postMessage");
@@ -65,3 +65,22 @@ it.each(MODES)("a rotation commit reads only the rotation tween's channels (%s)"
expect(read).toEqual({ rotation: 10 });
await preview.page.close();
});
// fails without the fix: a read right after a seek found no sealed values yet and the commit got none of GSAP's.
it.each(MODES)(
"a commit's read waits for the frame's values at the playhead (%s)",
async (mode) => {
const preview = await openPreview(browser, studioBundle, previewHtml(), mode);
await preview.seek(1);
const read = await preview.page.evaluate(() => {
const studio = (window as unknown as { hfStudio: Reads }).hfStudio;
const iframe = document.querySelector("iframe")!;
studio.usePlayerStore.setState({ currentTime: 1, isPlaying: false });
return studio.readAllAnimatedProperties(iframe, "#box", {
properties: { rotation: 20 },
} as never);
});
expect(read).toEqual({ rotation: 10 });
await preview.page.close();
},
);