Skip to content

Commit 368ae64

Browse files
committed
perf(core): a scene an eager scene reads stays eager, however far out it starts
1 parent 8ef62ad commit 368ae64

3 files changed

Lines changed: 73 additions & 11 deletions

File tree

‎packages/core/src/compiler/htmlBundler.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3303,7 +3303,7 @@ ${["a", "b", "c", "d", "e", "f"].map((id, i) => ` <div data-composition-id="$
33033303
const eager = (id: string) =>
33043304
doc.querySelector(`div[data-hf-scene="${id}"]`)?.getAttribute("data-hf-scene-eager");
33053305
expect(["a", "b", "c", "d", "e", "f"].map(eager)).toEqual([
3306-
null,
3306+
"d reads it",
33073307
"its script uses window.__bSeq =",
33083308
"its script uses documentElement",
33093309
"its script names a",
@@ -3316,6 +3316,37 @@ ${["a", "b", "c", "d", "e", "f"].map((id, i) => ` <div data-composition-id="$
33163316
);
33173317
});
33183318

3319+
it("keeps every scene an eager scene reads eager too, however far out it starts", async () => {
3320+
const scene = (id: string, script: string) =>
3321+
`<template id="${id}-template"><div data-composition-id="${id}"><p>${id}</p>
3322+
<script>${script}</script></div></template>`;
3323+
const dir = makeTempProject({
3324+
"index.html": `<!doctype html><html><head></head><body>
3325+
<div data-composition-id="main" data-width="1920" data-height="1080" data-duration="60">
3326+
<div data-composition-id="b" data-composition-src="compositions/b.html" data-start="30" data-duration="5"></div>
3327+
<div data-composition-id="a" data-composition-src="compositions/a.html" data-start="0" data-duration="5"></div>
3328+
<div data-composition-id="c" data-composition-src="compositions/c.html" data-start="40" data-duration="5"></div>
3329+
<div data-composition-id="d" data-composition-src="compositions/d.html" data-start="50" data-duration="5"></div>
3330+
</div></body></html>`,
3331+
"compositions/b.html": scene("b", 'window.__timelines["b"] = window.__timelines["c"];'),
3332+
"compositions/a.html": scene(
3333+
"a",
3334+
'document.title = String(Boolean(window.__timelines["b"]));',
3335+
),
3336+
"compositions/c.html": scene("c", 'window.__timelines["c"] = gsap.timeline();'),
3337+
"compositions/d.html": scene("d", 'window.__timelines["d"] = gsap.timeline();'),
3338+
});
3339+
const doc = parseHTML(await bundleToSingleHtml(dir, { sceneParts: true })).document;
3340+
const eager = (id: string) =>
3341+
doc.querySelector(`div[data-hf-scene="${id}"]`)?.getAttribute("data-hf-scene-eager");
3342+
expect(["a", "b", "c", "d"].map(eager)).toEqual([
3343+
"its script names b",
3344+
"its script names c",
3345+
"b reads it",
3346+
null,
3347+
]);
3348+
});
3349+
33193350
it("marks a scene whose own script leaves work running as not swappable, and only that scene", async () => {
33203351
const dir = makeTempProject({
33213352
"index.html": `<!doctype html><html><head>

‎packages/core/src/compiler/inlineSubCompositions.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ export function inlineSubCompositions(
754754
};
755755
}
756756

757-
/** A scene script that names another scene's composition id reads what that scene's script left. */
757+
/** A scene script that names another scene's composition id reads what that scene's script left: both run at boot. */
758758
function keepScenesNamingOthersEager(
759759
sceneHosts: Map<string, Element>,
760760
sceneLiterals: Map<string, Set<string>>,
@@ -766,14 +766,23 @@ function keepScenesNamingOthersEager(
766766
),
767767
);
768768
const ids = new Map([...sceneHosts].map(([scene, host]) => [scene, idsOf(host)]));
769-
for (const [scene, literals] of sceneLiterals) {
770-
const host = sceneHosts.get(scene);
771-
const own = ids.get(scene);
772-
if (!host || !own || host.hasAttribute(SCENE_EAGER_ATTR)) continue;
773-
const named = [...literals].find(
774-
(literal) =>
775-
literal && !own.has(literal) && [...ids.values()].some((other) => other.has(literal)),
776-
);
777-
if (named) host.setAttribute(SCENE_EAGER_ATTR, `its script names ${named}`);
769+
const providersOf = (scene: string) => {
770+
const own = ids.get(scene) ?? new Set<string>();
771+
const literals = [...(sceneLiterals.get(scene) ?? [])].filter((l) => l && !own.has(l));
772+
return [...ids].filter(([, other]) => literals.some((l) => other.has(l))).map(([id]) => id);
773+
};
774+
for (const [scene, host] of sceneHosts) {
775+
const named = providersOf(scene)[0];
776+
if (named && !host.hasAttribute(SCENE_EAGER_ATTR))
777+
host.setAttribute(SCENE_EAGER_ATTR, `its script names ${named}`);
778+
}
779+
const eager = [...sceneHosts].filter(([, host]) => host.hasAttribute(SCENE_EAGER_ATTR));
780+
for (const [reader] of eager) {
781+
for (const provider of providersOf(reader)) {
782+
const host = sceneHosts.get(provider);
783+
if (!host || host.hasAttribute(SCENE_EAGER_ATTR)) continue;
784+
host.setAttribute(SCENE_EAGER_ATTR, `${reader} reads it`);
785+
eager.push([provider, host]);
786+
}
778787
}
779788
}

‎packages/core/src/runtime/heldScenes.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,34 @@ describe("scene scripts held until the playhead nears", () => {
6464
document.head.innerHTML = "";
6565
document.body.innerHTML = "";
6666
delete document.body.dataset.ran;
67+
delete document.body.dataset.saw;
6768
window.__timelines = {};
6869
delete window.__player;
6970
delete (window as { __hyperframeRuntimeBootstrapped?: boolean })
7071
.__hyperframeRuntimeBootstrapped;
7172
});
7273

74+
it("runs a far scene at boot when the compiler kept it eager for a near scene that reads it", async () => {
75+
(globalThis as { CSS?: { escape?: (v: string) => string } }).CSS ??= {};
76+
globalThis.CSS.escape ??= (v: string) => v;
77+
document.head.appendChild(
78+
Object.assign(document.createElement("meta"), { name: STUDIO_PREVIEW_MARK_META }),
79+
);
80+
document.body.innerHTML =
81+
`<div data-composition-id="main" data-root="true" data-start="0" data-duration="60" data-width="1920" data-height="1080">` +
82+
host("b", 30, ' data-hf-scene-eager="a reads it"') +
83+
host("a", 0, ' data-hf-scene-eager="its script names b"') +
84+
`</div>` +
85+
sceneScript("b") +
86+
`<script type="${AFTER_FONTS_SCRIPT_TYPE}" data-hf-scene="a">` +
87+
`document.body.dataset.saw = String((document.body.dataset.ran || "").includes("b "));</script>`;
88+
window.__timelines = { main: timeline(60) };
89+
vi.resetModules();
90+
await import("./entry");
91+
await vi.waitFor(() => expect(window.__player).toBeDefined());
92+
expect(document.body.dataset.saw).toBe("true");
93+
});
94+
7395
it("runs a far scene's scripts once a seek comes within the lead, and nests it at its start", async () => {
7496
const added = await boot(true);
7597
expect(ran()).toBe("near tied ");

0 commit comments

Comments
 (0)