diff --git a/packages/core/src/compiler/htmlBundler.test.ts b/packages/core/src/compiler/htmlBundler.test.ts index 396d2ef852..a80f3c25b1 100644 --- a/packages/core/src/compiler/htmlBundler.test.ts +++ b/packages/core/src/compiler/htmlBundler.test.ts @@ -3303,7 +3303,7 @@ ${["a", "b", "c", "d", "e", "f"].map((id, i) => `
`
{ + const scene = (id: string, script: string) => + ``; + const dir = makeTempProject({ + "index.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": ` diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index 4ed5c36f7a..f936549e65 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -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, sceneLiterals: Map>, @@ -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(); + 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]); + } } } diff --git a/packages/core/src/runtime/heldScenes.test.ts b/packages/core/src/runtime/heldScenes.test.ts index 5f70390fbe..c9b7f723c4 100644 --- a/packages/core/src/runtime/heldScenes.test.ts +++ b/packages/core/src/runtime/heldScenes.test.ts @@ -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 = + `
` + + host("b", 30, ' data-hf-scene-eager="a reads it"') + + host("a", 0, ' data-hf-scene-eager="its script names b"') + + `
` + + sceneScript("b") + + ``; + 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 ");