diff --git a/packages/studio/src/components/editor/previewGestureStarts.ts b/packages/studio/src/components/editor/previewGestureStarts.ts index 1cc55ec206..2724cca334 100644 --- a/packages/studio/src/components/editor/previewGestureStarts.ts +++ b/packages/studio/src/components/editor/previewGestureStarts.ts @@ -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) => 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): boolean => { const pointerDown = down as unknown as React.PointerEvent; 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; }; diff --git a/packages/studio/src/components/editor/previewOverlayRects.test.ts b/packages/studio/src/components/editor/previewOverlayRects.test.ts index e8ac3af3c4..0634755a7b 100644 --- a/packages/studio/src/components/editor/previewOverlayRects.test.ts +++ b/packages/studio/src/components/editor/previewOverlayRects.test.ts @@ -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"); diff --git a/packages/studio/src/hooks/gsapSealedReads.browser.test.ts b/packages/studio/src/hooks/gsapSealedReads.browser.test.ts index c67a3d5337..301d9700d7 100644 --- a/packages/studio/src/hooks/gsapSealedReads.browser.test.ts +++ b/packages/studio/src/hooks/gsapSealedReads.browser.test.ts @@ -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(); + }, +);