mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(core): a sealed snapshot carries the tweens GSAP's global timeline runs (#8774)
* test(core): a sealed snapshot lists tweens only GSAP's global timeline runs * fix(core): a sealed snapshot carries the tweens GSAP's global timeline runs
This commit is contained in:
@@ -135,6 +135,23 @@ describe("gsapSnapshot", () => {
|
||||
|
||||
// fails without the fix: GSAP's own state on a keyframes tween's vars tripped the nesting bound, so a sealed preview
|
||||
// read no GSAP values and Add keyframe at playhead added nothing.
|
||||
it("lists the tweens only GSAP's global timeline runs, once, beside the registered ones", async () => {
|
||||
document.body.innerHTML = `<div id="a"></div><div id="b"></div>`;
|
||||
realGsap.globalTimeline.clear();
|
||||
const tl = realGsap.timeline({ paused: true });
|
||||
tl.to("#a", { x: 100, duration: 1 }, 0);
|
||||
realGsap.to("#b", { opacity: 0.5, duration: 1, paused: true });
|
||||
Object.assign(window, { gsap: realGsap, __timelines: { main: tl } });
|
||||
|
||||
const snapshot = await gsapSnapshot();
|
||||
realGsap.globalTimeline.clear();
|
||||
|
||||
const handle = (id: string) => handleFor(document.getElementById(id)!);
|
||||
expect(snapshot.timelines[0]!.tweens.map((tween) => tween.targets)).toEqual([[handle("a")]]);
|
||||
expect(snapshot.globalTweens.map((tween) => tween.targets)).toEqual([[handle("b")]]);
|
||||
expect(snapshot.globalTweens[0]!.vars).toMatchObject({ opacity: 0.5 });
|
||||
});
|
||||
|
||||
it("reads a real GSAP keyframes tween", async () => {
|
||||
document.body.innerHTML = `<div id="box"></div>`;
|
||||
const tl = realGsap.timeline({ paused: true });
|
||||
|
||||
@@ -55,6 +55,7 @@ export interface GsapSnapshot {
|
||||
time: number | null;
|
||||
defaultEase: string | null;
|
||||
timelines: GsapSnapshotTimeline[];
|
||||
globalTweens: GsapSnapshotTween[];
|
||||
targets: GsapSnapshotTarget[];
|
||||
}
|
||||
|
||||
@@ -78,6 +79,9 @@ interface LivePropTween {
|
||||
interface LiveGsap {
|
||||
getProperty?: (target: Element, prop: string) => unknown;
|
||||
defaults?: () => { ease?: unknown };
|
||||
globalTimeline?: {
|
||||
getChildren?: (nested: boolean, tweens: boolean, timelines: boolean) => LiveTween[];
|
||||
};
|
||||
}
|
||||
interface LiveTimeline {
|
||||
getChildren?: (nested: boolean) => LiveTween[];
|
||||
@@ -125,9 +129,11 @@ export async function gsapSnapshot(): Promise<GsapSnapshot> {
|
||||
const gsap = frame.gsap;
|
||||
const reader = new SnapshotReader();
|
||||
const timelines: GsapSnapshotTimeline[] = [];
|
||||
const held = new Set<LiveTween>();
|
||||
for (const [id, live] of Object.entries(frame.__timelines ?? {})) {
|
||||
if (typeof live?.getChildren !== "function") continue;
|
||||
const tweens = live.getChildren(true).filter((child) => typeof child.targets === "function");
|
||||
for (const tween of tweens) held.add(tween);
|
||||
if (timelines.length >= MAX_SNAPSHOT_TWEENS) throw new SnapshotBound("timelines");
|
||||
timelines.push({
|
||||
id: capped(id),
|
||||
@@ -136,11 +142,15 @@ export async function gsapSnapshot(): Promise<GsapSnapshot> {
|
||||
tweens: tweens.map((tween) => reader.tween(tween)),
|
||||
});
|
||||
}
|
||||
const globalTweens = (gsap?.globalTimeline?.getChildren?.(true, true, false) ?? [])
|
||||
.filter((child) => typeof child.targets === "function" && !held.has(child))
|
||||
.map((tween) => reader.tween(tween));
|
||||
const time = frame.__player?.getTime?.();
|
||||
return {
|
||||
time: typeof time === "number" && Number.isFinite(time) ? time : null,
|
||||
defaultEase: text(gsap?.defaults?.().ease),
|
||||
timelines,
|
||||
globalTweens,
|
||||
targets: reader.targets(gsap),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ const snapshot = (over: Record<string, unknown> = {}) => ({
|
||||
time: 0.5,
|
||||
defaultEase: null,
|
||||
timelines: [{ id: "main", time: 0.5, defaultEase: null, tweens: [tween] }],
|
||||
globalTweens: [],
|
||||
targets: [target],
|
||||
...over,
|
||||
});
|
||||
@@ -66,6 +67,8 @@ it.each([
|
||||
snapshot({ targets: Array.from({ length: 513 }, () => target) }),
|
||||
],
|
||||
["a value that is not a number", snapshot({ targets: [{ ...target, values: { x: "7" } }] })],
|
||||
["no list of global tweens", snapshot({ globalTweens: undefined })],
|
||||
["a global tween off its shape", snapshot({ globalTweens: [{ ...tween, targets: ["x"] }] })],
|
||||
[
|
||||
"a tween start that is not finite",
|
||||
snapshot({
|
||||
|
||||
@@ -44,6 +44,7 @@ const HANDLE = "0123456789abcdef:1";
|
||||
const snapshot = (vars: Record<string, number> | null) => ({
|
||||
time: 0.5,
|
||||
defaultEase: null,
|
||||
globalTweens: [],
|
||||
timelines: [
|
||||
{
|
||||
id: "main",
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// Real Chrome: a tween only GSAP's global timeline runs reaches a sealed snapshot as a same-origin one.
|
||||
import { readFileSync } from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import type { Browser } from "puppeteer-core";
|
||||
import { afterAll, beforeAll, expect, it, vi } from "vitest";
|
||||
import { closeTestChrome, launchTestChrome } from "../../tests/chromeTestUtils";
|
||||
import { buildStudioBundle, openPreview } from "../../tests/sealedPreviewHarness";
|
||||
|
||||
vi.setConfig({ testTimeout: 60_000, hookTimeout: 60_000 });
|
||||
const require = createRequire(import.meta.url);
|
||||
const MODES = process.env.HF_PARITY_MODE ? [process.env.HF_PARITY_MODE] : ["unsealed", "sealed"];
|
||||
let browser: Browser;
|
||||
let studioBundle: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
browser = await launchTestChrome();
|
||||
studioBundle = await buildStudioBundle([
|
||||
`export { queryPreviewElements } from "./player/lib/previewElements";`,
|
||||
`export { readGsapSnapshot } from "./hooks/gsapSnapshotReply";`,
|
||||
]);
|
||||
});
|
||||
|
||||
afterAll(() => closeTestChrome(browser));
|
||||
|
||||
function previewHtml(): string {
|
||||
const gsap = readFileSync(require.resolve("gsap/dist/gsap.min.js"), "utf8");
|
||||
const runtime = readFileSync(require.resolve("@hyperframes/core/runtime"), "utf8");
|
||||
return `<!doctype html><html><head><meta charset="UTF-8" />
|
||||
<meta name="hyperframes-studio-preview" content="1" />
|
||||
</head><body>
|
||||
<div id="root" data-composition-id="main" data-start="0" data-duration="4" data-width="1920" data-height="1080">
|
||||
<h1 id="a" data-start="0" data-duration="4">A</h1>
|
||||
<h1 id="b" data-start="0" data-duration="4">B</h1>
|
||||
</div>
|
||||
<script>${gsap}</script>
|
||||
<script>
|
||||
window.__timelines = window.__timelines || {};
|
||||
var tl = gsap.timeline({ paused: true });
|
||||
tl.to("#a", { x: 100, duration: 2 }, 0);
|
||||
window.__timelines.main = tl;
|
||||
gsap.to("#b", { color: "rgb(255, 0, 0)", duration: 2, paused: true });
|
||||
</script>
|
||||
<script>${runtime}</script></body></html>`;
|
||||
}
|
||||
|
||||
it.each(MODES)(
|
||||
"a tween only GSAP's global timeline runs is in the snapshot once, in a %s preview",
|
||||
async (mode) => {
|
||||
const preview = await openPreview(browser, studioBundle, previewHtml(), mode);
|
||||
try {
|
||||
const read = await preview.page.evaluate(async () => {
|
||||
type Snapshot = {
|
||||
timelines: Array<{ tweens: Array<{ targets: string[] }> }>;
|
||||
globalTweens: Array<{ targets: string[]; vars: Record<string, unknown> }>;
|
||||
};
|
||||
type Studio = {
|
||||
previewRequest(
|
||||
iframe: HTMLIFrameElement,
|
||||
kind: string,
|
||||
params: unknown,
|
||||
): Promise<unknown>;
|
||||
readGsapSnapshot(value: unknown): Snapshot | null;
|
||||
queryPreviewElements(
|
||||
iframe: HTMLIFrameElement,
|
||||
selector: string,
|
||||
): Promise<{ matches: Array<{ handle: string }> } | null>;
|
||||
};
|
||||
const studio = (window as unknown as { hfStudio: Studio }).hfStudio;
|
||||
const iframe = document.querySelector("iframe")!;
|
||||
const handle = async (selector: string) =>
|
||||
(await studio.queryPreviewElements(iframe, selector))!.matches[0]!.handle;
|
||||
const snapshot = studio.readGsapSnapshot(
|
||||
await studio.previewRequest(iframe, "gsap-snapshot", {}),
|
||||
)!;
|
||||
const [a, b] = [await handle("#a"), await handle("#b")];
|
||||
return {
|
||||
registered: snapshot.timelines.flatMap((line) => line.tweens.map((t) => t.targets)),
|
||||
global: snapshot.globalTweens.map((t) => t.targets),
|
||||
globalVars: snapshot.globalTweens.map((t) => t.vars),
|
||||
a,
|
||||
b,
|
||||
};
|
||||
});
|
||||
expect(read.registered.filter((targets) => targets.length)).toEqual([[read.a]]);
|
||||
expect(read.global).toEqual([[read.b]]);
|
||||
expect(read.globalVars).toHaveLength(1);
|
||||
expect(read.globalVars[0]).toMatchObject({ color: "rgb(255, 0, 0)" });
|
||||
} finally {
|
||||
await preview.page.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -29,10 +29,12 @@ export function readGsapSnapshot(value: unknown): Snapshot | null {
|
||||
if (!isFields(value) || !isNumberOrNull(value.time) || !isTextOrNull(value.defaultEase))
|
||||
return null;
|
||||
const budget: Budget = { tweens: MAX_SNAPSHOT_TWEENS, fields: MAX_SNAPSHOT_FIELDS };
|
||||
const { timelines, targets } = value;
|
||||
const { timelines, globalTweens, targets } = value;
|
||||
const ok =
|
||||
isList(timelines, MAX_SNAPSHOT_TWEENS) &&
|
||||
timelines.every((line) => isTimeline(line, budget)) &&
|
||||
isList(globalTweens, MAX_SNAPSHOT_TWEENS) &&
|
||||
globalTweens.every((tween) => isTween(tween, budget)) &&
|
||||
isList(targets, MAX_SNAPSHOT_TARGETS) &&
|
||||
targets.every(isTarget);
|
||||
return ok ? (value as unknown as Snapshot) : null;
|
||||
|
||||
@@ -107,6 +107,7 @@ it("reads the frame's GSAP values again once a saved script reruns in place", as
|
||||
time: 2,
|
||||
defaultEase: null,
|
||||
timelines: [{ id: "main", time: 2, defaultEase: null, tweens: [] }],
|
||||
globalTweens: [],
|
||||
targets: [{ handle: "0123456789abcdef:1", id: "b", className: "", values: { scaleX } }],
|
||||
});
|
||||
let scaleX = 1.125;
|
||||
|
||||
Reference in New Issue
Block a user