Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371/** * Dev-only gallery churn probe — the `[grid]` line. * * "The gallery keeps refreshing itself" is not one bug, it is four that look * identical on screen, and they are only distinguishable by *which* reactive * scope is running: * * - `list-rows` / `grid-cells` re-running means the **list's** index moved under * the grid (the cells are derived from those rows); * - `grid-rows` re-running is the worst case, because `gridRows()` mints a fresh * array per row and `<For>` keys rows by array *identity* — one recompute * disposes every row and cell in the window and builds them again; * - an `<img>` being created is a cell that no longer exists and was reborn, and * each one is a fresh `getCoverArt` off the network; * - a width write means the ResizeObserver is measuring the grid against a layout * the grid just changed — a measurement loop. * * So this counts each of those separately and prints **one line per burst**, with * the burst's time span and whether any input preceded it. A line that says * `idle` while `imgs` repeats the same covers is the bug being reported, named. * * Deliberately quiet: a single recompute (a keystroke in the filter, a resize) is * not churn and prints nothing — see `churny()`. Prod builds strip all of it, * every entry point being behind `import.meta.env.DEV`. * * For the causality chain behind these counts, use the dev server's attribution * instead of guessing from them: `POST /__solid/diagnostics {"method":"begin"}`, * wait, then `{"method":"whyDidRun","params":{"name":"grid-rows"}}`. The scopes * this probe counts are the same scopes those queries name. */
/** One console line per burst of this many ms. */const WINDOW_MS = 500;/** Older than this, and nothing the person did explains the burst. */const IDLE_MS = 1500;/** A runaway loop must not eat the console (or the tab that prints it). */const MAX_LINES = 60;
/** scope name → runs, of which `wasted` ran on unchanged inputs. */type Spans = Map<string, { runs: number; wasted: number }>;
interface Burst { /** ms when the burst started and how long it has been collecting. */ t0: number; t1: number; spans: Spans; /** AlbumCell bodies run — each one is a cell brought into existence. */ cells: number; /** `<img>` nodes created, and how many of them asked for a cover seen before. */ imgs: number; repeats: number; /** Covers asked for but held behind the motion, and the slowest one that landed. */ coversHeld: number; coversLanded: number; slowestMs: number; /** ResizeObserver deliveries that changed the measured width. */ widths: number; /** Short facts, in arrival order, printed as the tail of the line. */ notes: string[];}
function burst(t: number): Burst { return { t0: t, t1: t, spans: new Map(), cells: 0, imgs: 0, repeats: 0, coversHeld: 0, coversLanded: 0, slowestMs: 0, widths: 0, notes: [], };}
let active: Burst | null = null;let timer: ReturnType<typeof setTimeout> | undefined = undefined;let lines = 0;/** Every cover URL this page has asked for; a repeat is a cell re-created, not revealed. */let seen = new Set<string>();let lastInput = -Infinity;let watching = false;/** What last went to the console, for `dump()` when the interesting burst was below the bar. */let lastLine = "(nothing printed yet)";let muted = false;
function now(): number { return typeof performance === "undefined" ? 0 : performance.now();}
function open(): Burst { if (!active) active = burst(now()); return active;}
function note(s: Burst, fact: string): void { if (s.notes.length < 6 && !s.notes.includes(fact)) s.notes.push(fact);}
/** * A burst is worth a line when it repeats itself. One recompute is a filter * keystroke or a window drag; the thing being hunted is a grid that rebuilds * while nobody is touching it, and that shows up as several recomputes in one * window, covers asked for twice, or the observer measuring more than once. */function churny(s: Burst): boolean { let runs = 0; let wasted = 0; for (const r of s.spans.values()) { runs += r.runs; wasted += r.wasted; } return ( runs >= 2 || wasted > 0 || s.repeats > 0 || s.widths >= 2 || s.slowestMs >= 400 || (s.coversHeld >= 8 && s.coversLanded === 0) );}
function flush(): void { timer = undefined; const s = active; active = null; if (!s || muted || !churny(s)) return; if (lines >= MAX_LINES) return; lines++;
const spans = [...s.spans.entries()] .map(([name, r]) => `${name} ×${r.runs}${r.wasted ? ` (${r.wasted} wasted)` : ""}`) .join(" · "); const idle = s.t1 - lastInput > IDLE_MS || s.t1 < lastInput ? "idle" : `${Math.round(s.t1 - lastInput)}ms after input`; const asked = s.imgs ? ` · covers ${s.imgs} asked, ${s.coversLanded} landed` + `${s.slowestMs >= 60 ? `, slowest ${Math.round(s.slowestMs)}ms` : ""}` + `${s.coversHeld ? `, ${s.coversHeld} held` : ""}` + `${s.repeats ? `, ${s.repeats} re-asked` : ""}` : ""; const width = s.widths ? ` · width ×${s.widths}` : "";
lastLine = `[grid] ${spans || "no scope re-ran"} · cells ${s.cells}${asked}${width}${paneState()} — ` + `${idle} · ${Math.round(s.t0)}→${Math.round(s.t1)}ms` + `${s.notes.length ? ` — ${s.notes.join(" · ")}` : ""}`; console.debug(`${lastLine}${hint(s)}`);}
/** * Whether the gallery was on screen when the burst happened. * * Both panes stay mounted — that is what lets a cover click scroll the list — so * the grid keeps re-rendering while the reader is looking at the list. A burst * marked `hidden pane` is waste nobody sees but everybody pays for, and it points * upstream at the shared rows rather than at anything the grid itself does. Read * off the DOM at flush time, so it costs nothing until a line is being printed. */function paneState(): string { if (typeof document === "undefined") return ""; const pane = document.querySelector(".album-grid")?.closest(".lib-pane"); return pane?.classList.contains("lib-pane--inactive") ? " · hidden pane" : "";}
/** * The shape of the burst, read as a next place to look. These are hints from the * counts, not conclusions — `whyDidRun` on the named scope is the evidence. */function hint(s: Burst): string { const rows = s.spans.get("grid-rows"); const out: string[] = []; if (rows && rows.runs >= 2 && s.cells === 0) { // The cheap outcome, and worth saying out loud: the derivation ran, and // <For> was handed the array it already had. out.push("grid-rows re-ran and was retained — nothing in the window was rebuilt"); } if (s.cells > 0 && s.repeats > 0 && s.repeats >= s.imgs / 2) { out.push( "most covers are being re-requested, so cells are being re-created rather than scrolled into view", ); } if (rows && rows.wasted === rows.runs && rows.runs >= 2 && s.cells > 0) { out.push( "grid-rows republished an identical window: an upstream memo is churning the cells it was given", ); } if (s.widths >= 2) out.push("the observer is re-measuring width — check what changes the grid's own width"); if (s.slowestMs >= 400 && s.coversHeld === 0) { out.push( `a cover stood ${Math.round(s.slowestMs)}ms with nothing held behind it: that wait is the server's thumbnail and the connection pool, not this view`, ); } if (s.coversHeld >= 8) { out.push( `${s.coversHeld} covers were held behind the motion and asked for when it settled — the queue guard working`, ); } if (s.spans.has("list-rows")) out.push("the list's rows moved: the grid is only following them"); return out.length ? `\n → ${out.join("; ")}` : "";}
function arm(): void { if (timer !== undefined) return; // A burst is flushed a fixed distance after it *starts*, not after its last // event: a loop that never pauses would otherwise never be printed at all. timer = setTimeout(flush, WINDOW_MS);}
/** * A scope ran. `changed` is the caller's answer to "did its inputs differ?" — * a run with `changed: false` is pure waste, and a burst made of nothing but * waste is the signature of an unstable memo upstream rather than a real update. */export function traceRecompute(scope: string, changed: boolean, detail?: string): void { if (!import.meta.env.DEV) return; const s = open(); s.t1 = now(); const run = s.spans.get(scope) ?? { runs: 0, wasted: 0 }; run.runs++; if (!changed) run.wasted++; s.spans.set(scope, run); if (detail) note(s, `${scope}: ${detail}`); arm();}
/** An AlbumCell body ran — a cell came into existence. */export function traceCellMount(): void { if (!import.meta.env.DEV) return; open().cells++; arm();}
/** * A cover was asked for but held, because the reader was still moving. * * Counted at the cell's birth rather than on a timer: the question the line * answers is "did that fling get a queue built behind it", and a cell born * mid-fling and held is precisely the request that used to go out. */export function traceCoverHeld(): void { if (!import.meta.env.DEV) return; open().coversHeld++; arm();}
/** When each outstanding cover was asked for, so its landing can be timed. */const requestedAt = new Map<string, number>();const REQUESTED_CAP = 2048;
/** An `<img>` was created. A URL seen before is a cell that was destroyed and rebuilt. */export function traceImgMount(src: string): void { if (!import.meta.env.DEV) return; const s = open(); s.imgs++; if (seen.has(src)) s.repeats++; else seen.add(src); if (src) { if (requestedAt.size >= REQUESTED_CAP) { const oldest = requestedAt.keys().next(); if (!oldest.done) requestedAt.delete(oldest.value); } requestedAt.set(src, now()); } // Armed like everything else — and it matters. It was not armed at first, on // the theory that a mount always travels with a recompute that schedules the // print; a burst opened by an image alone then sat unflushed until some later // recompute armed it, and reported a span fifteen seconds wide. // // Being armed is not being printed: scrolling the gallery creates images with // URLs nobody has asked for, which is not churn, and `churny()` says so. arm();}
/** * A cover landed, timed from the moment its `<img>` was created to the moment * its bytes were on screen. * * That span is the number the reader feels, and separating it from everything * else is the point: a long one with nothing held is the server resampling a * thumbnail on a half-dozen-connection pool, which this view cannot fix, while a * long one with hundreds held was the view queueing work behind itself, which it * now does not. */export function traceCoverLanded(src: string): void { if (!import.meta.env.DEV) return; const asked = requestedAt.get(src); if (asked === undefined) return; requestedAt.delete(src); const s = open(); s.coversLanded++; const ms = now() - asked; if (ms > s.slowestMs) s.slowestMs = ms; arm();}
/** The grid's ResizeObserver delivered a new width. */export function traceWidthWrite(px: number): void { if (!import.meta.env.DEV) return; const s = open(); s.widths++; note(s, `width ${px}px`); arm();}
/** * Scroll counts as input even though it is not a key or a click: scrolling the * gallery is the one reason to rebuild rows that a person would call normal, and * the line has to be able to say "this burst followed a scroll" as opposed to * "nothing happened". */function watchInputs(): void { if (watching || typeof window === "undefined" || typeof document === "undefined") return; watching = true; const stamp = () => { lastInput = now(); }; for (const type of ["pointerdown", "keydown", "wheel", "touchstart", "scroll", "resize"]) { window.addEventListener(type, stamp, { capture: true, passive: true }); } // So the very first burst is not blamed on a person who has only just opened // the page: loading the library legitimately builds the grid once. stamp();}
export interface GridTraceConsole { /** Forget the counters (and which covers were already asked for). */ reset(): void; /** Print the current burst and the last line, whether or not it earned a line. */ dump(): void; /** `__gridTrace.on(false)` to stop the lines without a reload. */ on(flag: boolean): void;}
if (import.meta.env.DEV && typeof window !== "undefined") { watchInputs(); const api: GridTraceConsole = { reset: () => { active = null; if (timer !== undefined) clearTimeout(timer); timer = undefined; lines = 0; seen = new Set(); console.debug("[grid] counters reset"); }, dump: () => { const s = active; console.debug( `[grid] last line: ${lastLine}\n[grid] open burst: ` + (s ? `${[...s.spans.entries()].map(([n, r]) => `${n} ×${r.runs}`).join(" · ")} · ` + `cells ${s.cells} · covers ${s.imgs} asked, ${s.coversLanded} landed ` + `(slowest ${Math.round(s.slowestMs)}ms, ${s.coversHeld} held, ${s.repeats} re-asked) · ` + `width ×${s.widths} · ${s.notes.length} notes` : "none") + ` · printed ${lines}/${MAX_LINES}`, ); }, on: (flag) => { muted = !flag; console.debug(`[grid] ${flag ? "reporting" : "muted"}`); }, }; (window as unknown as { __gridTrace: GridTraceConsole }).__gridTrace = api;}