Files
hyperframes/packages/core/src/runtime/farScenes.test.ts
T
Miguel Ángel 70187a13b5 perf(core): the preview mounts and draws only the scenes near the playhead (#6189)
* perf(core): skip rendering preview scenes far from the playhead

* refactor(core): split the far-scene pass into small helpers

* perf(core): a first render reveals only the clips it reaches

* fix(core): reveal the clips holding first-rendered tweens, wherever their window is

* feat(core): a scene may load a project helper and wait for fonts and still swap

* perf(core): the preview mounts only the scenes near the playhead

* perf(core): a seek waits only for the scene it lands in; neighbours mount one per turn

* feat(studio): selecting a clip in an unmounted scene mounts that scene first

* fix(core): a source a play and a seek both need is fetched and decoded once

* fix(core): a scene mounting during play joins the mix without restarting what plays

* fix(studio): selecting a clip waits on a scene mount only in a preview that unmounts scenes

* fix(core): far scenes still mount after an edit, and an edit to a parked scene keeps its styles

* perf(core): web audio schedules a clip only as the playhead comes near it

* chore(studio): trim the scene hold helper's comments to its package's share

* fix(core): a scene the preview unmounts is freed, not kept by audio, caches or old roots

* fix(studio): an unmounted scene's rows and their state come from the markup it mounts with

* perf(core): the preview mounts scenes ahead of play and keeps recent ones mounted

* refactor(core): keep the far-scene neighbour rule private to its module

* style(core): format the far-scene neighbour rule

* perf(core): the preview unmounts scenes the playhead leaves, as kept ones slowed each mount

* fix(core): scenes that measure text, call their own animate or wait on font loads can unmount

* fix(core): each preview scene carries the helpers it loads, so it mounts on its own

* fix(core): a mounted scene shows though its stand-in was hidden by display

* fix(core): a helper scenes share runs once for the film, and a remounted scene keeps showing

* fix(core): a scene that also publishes its timeline under another id stays mounted

* feat(core): scenes that draw in 2D or chain the timeline they register can unmount

* perf(core): a play frame that draws nothing new skips the hidden-clip lookup

* fix(core): a GSAP file copied into a project counts as the library for scene unmounting

* style(core): drop an unneeded escape in the registered-chain pattern

* chore: core and producer size budgets make room for the preview scene window

* fix(core): a scene that draws on a 2D canvas stays mounted

* fix(core): a scene the preview unmounts lets go of timelines it built after a font wait

* perf(core): a scene leaving the playhead keeps its last rendered size, no forced layout at the cut

* refactor(core): the far-scene skip keeps last-render boxes in their own helper

* fix(core): a mounting scene never records the runtime's root timeline as its own

* chore(producer): raise the size budget to the measured size with the preview runtime inlined

* docs(core): keep the far-scene and scene-memory comments within the comment limits

* perf(core): style each hidden clip visible once ahead of its cut, so the cut frame does not

* fix(core): a seek into a scene still mounting as a neighbour waits for that mount

* chore(core): keep comment share and size budgets to main plus the measured core growth
2026-10-25 19:39:14 +00:00

114 lines
4.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { createFarSceneSkip } from "./farScenes";
import { resetRuntimeFixtureDom } from "./runtimeSeekFixture.test-helpers";
const FAR = "data-hf-far-scene";
// Six 2 s scenes back to back, plus a scene style part that is not a host.
function film(tagged = true): HTMLElement[] {
resetRuntimeFixtureDom();
const tag = (i: number) => (tagged ? ` data-hf-scene="s${i}"` : "");
document.body.innerHTML =
`<style${tag(0)}>p{}</style>` +
Array.from(
{ length: 6 },
(_, i) => `<div id="s${i}"${tag(i)} data-start="${2 * i}" style="position:absolute"></div>`,
).join("");
return Array.from(document.querySelectorAll<HTMLElement>("div"));
}
const spanOf = (host: Element) => {
const start = Number(host.getAttribute("data-start"));
return { start, end: start + 2 };
};
const farIds = () => Array.from(document.querySelectorAll(`[${FAR}]`), (el) => el.id);
afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
document.head.innerHTML = "";
document.body.innerHTML = "";
});
describe("createFarSceneSkip", () => {
it("skips every scene but the playing one and its neighbours, following play, seek and scrub", () => {
film();
const skip = createFarSceneSkip(spanOf);
skip(1);
expect(farIds()).toEqual(["s2", "s3", "s4", "s5"]);
// Played across the cut: both scenes play at it, so each keeps its other neighbour.
for (let t = 1; t <= 2; t += 1 / 30) skip(t);
skip(2);
expect(farIds()).toEqual(["s3", "s4", "s5"]);
skip(9);
expect(farIds()).toEqual(["s0", "s1", "s2"]);
for (const t of [7, 3, 5]) skip(t);
expect(farIds()).toEqual(["s0", "s4", "s5"]);
expect(document.querySelector(`style[${FAR}]`)).toBeNull();
});
it("keeps a scene with no known start rendered", () => {
film();
const skip = createFarSceneSkip((host) => (host.id === "s5" ? null : spanOf(host)));
skip(0);
expect(farIds()).toEqual(["s2", "s3", "s4"]);
});
it("keeps a scene host in flow rendered, since containment would move what it holds", () => {
film()[3]!.style.position = "relative";
createFarSceneSkip(spanOf)(0);
expect(farIds()).toEqual(["s2", "s4", "s5"]);
});
it("does nothing on a page without scene hosts, as in a render", () => {
film(false);
createFarSceneSkip(spanOf)(1);
expect(farIds()).toEqual([]);
expect(document.head.querySelector("style")).toBeNull();
});
it("holds a skipped scene at the size it rendered at", () => {
const [, , s2] = film();
Object.defineProperty(s2, "offsetWidth", { value: 640 });
Object.defineProperty(s2, "offsetHeight", { value: 360 });
createFarSceneSkip(spanOf)(0);
const css = document.head.querySelector("style")?.textContent ?? "";
expect(css).toContain(`[${FAR}] { content-visibility: hidden; }`);
expect(css).toContain(`[data-hf-scene="s2"][${FAR}] { contain-intrinsic-size: 640px 360px; }`);
});
it("takes a leaving scene's size and position from its last render, reading nothing at the cut", () => {
const hosts = film();
let report: (entries: unknown[]) => void = () => {};
vi.stubGlobal(
"ResizeObserver",
class {
constructor(callback: (entries: unknown[]) => void) {
report = callback;
}
observe() {}
unobserve() {}
},
);
const skip = createFarSceneSkip(spanOf);
skip(1);
// What the browser reports after the next layout.
report(
hosts.map((target) => ({ target, borderBoxSize: [{ inlineSize: 640, blockSize: 360 }] })),
);
// The cut's own changes leave style dirty: a read here would force a full style and layout pass.
const computed = vi.spyOn(window, "getComputedStyle");
for (const host of hosts)
Object.defineProperty(host, "offsetWidth", {
get: () => {
throw new Error("read at the cut");
},
});
skip(5);
expect(farIds()).toEqual(["s0", "s4", "s5"]);
expect(computed).not.toHaveBeenCalled();
expect(document.head.querySelector("style")?.textContent).toContain(
`[data-hf-scene="s0"][${FAR}] { contain-intrinsic-size: 640px 360px; }`,
);
});
});