perf(core): a scene an eager scene reads stays eager, however far out it starts

This commit is contained in:
Miguel Angel Simon Sierra
2026-10-17 03:22:44 -07:00
parent 8ef62adeec
commit 368ae644ba
3 changed files with 73 additions and 11 deletions
+32 -1
View File
@@ -3303,7 +3303,7 @@ ${["a", "b", "c", "d", "e", "f"].map((id, i) => ` <div data-composition-id="$
const eager = (id: string) =>
doc.querySelector(`div[data-hf-scene="${id}"]`)?.getAttribute("data-hf-scene-eager");
expect(["a", "b", "c", "d", "e", "f"].map(eager)).toEqual([
null,
"d reads it",
"its script uses window.__bSeq =",
"its script uses documentElement",
"its script names a",
@@ -3316,6 +3316,37 @@ ${["a", "b", "c", "d", "e", "f"].map((id, i) => ` <div data-composition-id="$
);
});
it("keeps every scene an eager scene reads eager too, however far out it starts", async () => {
const scene = (id: string, script: string) =>
`<template id="${id}-template"><div data-composition-id="${id}"><p>${id}</p>
<script>${script}</script></div></template>`;
const dir = makeTempProject({
"index.html": `<!doctype html><html><head></head><body>
<div data-composition-id="main" data-width="1920" data-height="1080" data-duration="60">
<div data-composition-id="b" data-composition-src="compositions/b.html" data-start="30" data-duration="5"></div>
<div data-composition-id="a" data-composition-src="compositions/a.html" data-start="0" data-duration="5"></div>
<div data-composition-id="c" data-composition-src="compositions/c.html" data-start="40" data-duration="5"></div>
<div data-composition-id="d" data-composition-src="compositions/d.html" data-start="50" data-duration="5"></div>
</div></body></html>`,
"compositions/b.html": scene("b", 'window.__timelines["b"] = window.__timelines["c"];'),
"compositions/a.html": scene(
"a",
'document.title = String(Boolean(window.__timelines["b"]));',
),
"compositions/c.html": scene("c", 'window.__timelines["c"] = gsap.timeline();'),
"compositions/d.html": scene("d", 'window.__timelines["d"] = gsap.timeline();'),
});
const doc = parseHTML(await bundleToSingleHtml(dir, { sceneParts: true })).document;
const eager = (id: string) =>
doc.querySelector(`div[data-hf-scene="${id}"]`)?.getAttribute("data-hf-scene-eager");
expect(["a", "b", "c", "d"].map(eager)).toEqual([
"its script names b",
"its script names c",
"b reads it",
null,
]);
});
it("marks a scene whose own script leaves work running as not swappable, and only that scene", async () => {
const dir = makeTempProject({
"index.html": `<!doctype html><html><head>
@@ -754,7 +754,7 @@ export function inlineSubCompositions(
};
}
/** A scene script that names another scene's composition id reads what that scene's script left. */
/** A scene script that names another scene's composition id reads what that scene's script left: both run at boot. */
function keepScenesNamingOthersEager(
sceneHosts: Map<string, Element>,
sceneLiterals: Map<string, Set<string>>,
@@ -766,14 +766,23 @@ function keepScenesNamingOthersEager(
),
);
const ids = new Map([...sceneHosts].map(([scene, host]) => [scene, idsOf(host)]));
for (const [scene, literals] of sceneLiterals) {
const host = sceneHosts.get(scene);
const own = ids.get(scene);
if (!host || !own || host.hasAttribute(SCENE_EAGER_ATTR)) continue;
const named = [...literals].find(
(literal) =>
literal && !own.has(literal) && [...ids.values()].some((other) => other.has(literal)),
);
if (named) host.setAttribute(SCENE_EAGER_ATTR, `its script names ${named}`);
const providersOf = (scene: string) => {
const own = ids.get(scene) ?? new Set<string>();
const literals = [...(sceneLiterals.get(scene) ?? [])].filter((l) => l && !own.has(l));
return [...ids].filter(([, other]) => literals.some((l) => other.has(l))).map(([id]) => id);
};
for (const [scene, host] of sceneHosts) {
const named = providersOf(scene)[0];
if (named && !host.hasAttribute(SCENE_EAGER_ATTR))
host.setAttribute(SCENE_EAGER_ATTR, `its script names ${named}`);
}
const eager = [...sceneHosts].filter(([, host]) => host.hasAttribute(SCENE_EAGER_ATTR));
for (const [reader] of eager) {
for (const provider of providersOf(reader)) {
const host = sceneHosts.get(provider);
if (!host || host.hasAttribute(SCENE_EAGER_ATTR)) continue;
host.setAttribute(SCENE_EAGER_ATTR, `${reader} reads it`);
eager.push([provider, host]);
}
}
}
@@ -64,12 +64,34 @@ describe("scene scripts held until the playhead nears", () => {
document.head.innerHTML = "";
document.body.innerHTML = "";
delete document.body.dataset.ran;
delete document.body.dataset.saw;
window.__timelines = {};
delete window.__player;
delete (window as { __hyperframeRuntimeBootstrapped?: boolean })
.__hyperframeRuntimeBootstrapped;
});
it("runs a far scene at boot when the compiler kept it eager for a near scene that reads it", async () => {
(globalThis as { CSS?: { escape?: (v: string) => string } }).CSS ??= {};
globalThis.CSS.escape ??= (v: string) => v;
document.head.appendChild(
Object.assign(document.createElement("meta"), { name: STUDIO_PREVIEW_MARK_META }),
);
document.body.innerHTML =
`<div data-composition-id="main" data-root="true" data-start="0" data-duration="60" data-width="1920" data-height="1080">` +
host("b", 30, ' data-hf-scene-eager="a reads it"') +
host("a", 0, ' data-hf-scene-eager="its script names b"') +
`</div>` +
sceneScript("b") +
`<script type="${AFTER_FONTS_SCRIPT_TYPE}" data-hf-scene="a">` +
`document.body.dataset.saw = String((document.body.dataset.ran || "").includes("b "));</script>`;
window.__timelines = { main: timeline(60) };
vi.resetModules();
await import("./entry");
await vi.waitFor(() => expect(window.__player).toBeDefined());
expect(document.body.dataset.saw).toBe("true");
});
it("runs a far scene's scripts once a seek comes within the lead, and nests it at its start", async () => {
const added = await boot(true);
expect(ran()).toBe("near tied ");