Skip to content

Commit 7752323

Browse files
committed
refactor(core): the far-scene skip keeps last-render boxes in their own helper
1 parent 73f344f commit 7752323

1 file changed

Lines changed: 45 additions & 30 deletions

File tree

‎packages/core/src/runtime/farScenes.ts‎

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,46 @@ function farFrom(timed: Timed[], time: number): Set<HTMLElement> {
2525
return new Set(timed.filter((_, i) => !near[i]).map((s) => s.host));
2626
}
2727

28+
type Box = { size: string; outOfFlow: boolean };
29+
30+
const boxOf = (host: Element, size: string): Box => ({
31+
size,
32+
outOfFlow: OUT_OF_FLOW.has(getComputedStyle(host).position),
33+
});
34+
35+
/**
36+
* Each host's box and position as of its last render, reported after layout: read at a cut, where the cut
37+
* leaves the page's style dirty, they would force a full style and layout pass first. A host not reported
38+
* yet is read directly.
39+
*/
40+
function createLastRender() {
41+
const rendered = new WeakMap<Element, Box>();
42+
const observer =
43+
typeof ResizeObserver === "function"
44+
? new ResizeObserver((entries) => {
45+
for (const { target, borderBoxSize } of entries) {
46+
const box = borderBoxSize?.[0];
47+
if (box)
48+
rendered.set(
49+
target,
50+
boxOf(target, `${Math.round(box.inlineSize)}px ${Math.round(box.blockSize)}px`),
51+
);
52+
}
53+
})
54+
: null;
55+
let watched: HTMLElement[] = [];
56+
return {
57+
watch(hosts: HTMLElement[]) {
58+
// An observed element stays alive with its observer, so a host that left the page is let go.
59+
for (const host of watched) if (!host.isConnected) observer?.unobserve(host);
60+
for (const host of hosts) observer?.observe(host);
61+
watched = hosts;
62+
},
63+
of: (host: HTMLElement): Box =>
64+
rendered.get(host) ?? boxOf(host, `${host.offsetWidth}px ${host.offsetHeight}px`),
65+
};
66+
}
67+
2868
/**
2969
* Preview scenes beyond the playing scene's neighbours stay mounted but skip style, layout and paint.
3070
* Each keeps the size it had when last rendered, so a script measuring its host reads the same box.
@@ -34,39 +74,18 @@ export function createFarSceneSkip(spanOf: (host: Element) => Span): (time: numb
3474
let hosts: HTMLElement[] | null = null;
3575
let sheet: HTMLStyleElement | null = null;
3676
const sizes = new Map<string, string>();
77+
const lastRender = createLastRender();
3778
const timedHosts = (list: HTMLElement[]): Timed[] =>
3879
list
3980
.map((host) => ({ host, span: spanOf(host) }))
4081
.filter((s): s is Timed => Number.isFinite(s.span?.start))
4182
.sort((a, b) => a.span.start - b.span.start);
42-
// Each host's box and position as of its last render, reported after layout: read at a cut, where the cut
43-
// leaves the page's style dirty, they would force a full style and layout pass first.
44-
const rendered = new WeakMap<Element, { size: string; outOfFlow: boolean }>();
45-
const observer =
46-
typeof ResizeObserver === "function"
47-
? new ResizeObserver((entries) => {
48-
for (const { target, borderBoxSize } of entries) {
49-
const box = borderBoxSize?.[0];
50-
if (!box) continue;
51-
rendered.set(target, {
52-
size: `${Math.round(box.inlineSize)}px ${Math.round(box.blockSize)}px`,
53-
outOfFlow: OUT_OF_FLOW.has(getComputedStyle(target).position),
54-
});
55-
}
56-
})
57-
: null;
58-
// Read directly only for a host not reported yet; measured before any is marked, so one layout serves them all.
59-
const lastRender = (host: HTMLElement) =>
60-
rendered.get(host) ?? {
61-
size: `${host.offsetWidth}px ${host.offsetHeight}px`,
62-
outOfFlow: OUT_OF_FLOW.has(getComputedStyle(host).position),
63-
};
64-
// True when a size changed.
83+
// Measured before any is marked, so one layout serves them all; true when a size changed.
6584
const measureLeaving = (far: Set<HTMLElement>): boolean => {
6685
let resized = false;
6786
for (const host of far) {
6887
if (host.hasAttribute(FAR)) continue;
69-
const { size, outOfFlow } = lastRender(host);
88+
const { size, outOfFlow } = lastRender.of(host);
7089
// Containment makes a host a containing block and margin boundary; only an out-of-flow one already is.
7190
if (!outOfFlow) {
7291
far.delete(host);
@@ -91,12 +110,8 @@ export function createFarSceneSkip(spanOf: (host: Element) => Span): (time: numb
91110
if (!sheet.isConnected) document.head.appendChild(sheet);
92111
};
93112
return (time) => {
94-
if (!hosts || hosts.some((host) => !host.isConnected)) {
95-
// An observed element stays alive with its observer, so a host that left the page is let go.
96-
for (const host of hosts ?? []) if (!host.isConnected) observer?.unobserve(host);
97-
hosts = Array.from(document.querySelectorAll<HTMLElement>(HOSTS));
98-
for (const host of hosts) observer?.observe(host);
99-
}
113+
if (!hosts || hosts.some((host) => !host.isConnected))
114+
lastRender.watch((hosts = Array.from(document.querySelectorAll<HTMLElement>(HOSTS))));
100115
if (hosts.length === 0) return;
101116
const far = farFrom(timedHosts(hosts), time);
102117
if (measureLeaving(far) || !sheet?.isConnected) writeSheet();

0 commit comments

Comments
 (0)