Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181/** * When a cover is worth asking for, and how big to ask for. * * The gallery asks per cell: every `<img>` that comes into existence is a * `getCoverArt`, and on a Navidrome that request is not a file read but a * thumbnail — the server resizes the picture on the way out. A fling down 1,703 * albums therefore puts several hundred resamples in front of the ~40 covers the * reader actually stopped to look at, on a connection pool half a dozen wide, and * the ones that matter arrive a second late. The placeholder hides the latency; * the queue is what makes it felt. * * So two questions are answered before a byte is asked for, and neither is asked * of the server: * * - **Is the reader still moving?** Past a few rows a second, a cover requested * now is a cover that will be off the screen before it lands. Hold, and ask * when it settles — the tile keeps its shape and initials meanwhile, so * nothing about holding is visible except the covers arriving together. * - **Have we had this one already?** A cover that loaded earlier in the session * is a memory-cache hit, so it has no queue to be behind: render it now and * let it be the thing that answers while its neighbours wait. * * And how big to ask for: `devicePixelRatio`, capped at 2. The flat 2× this used * to send asked a 1× screen for four times the pixels of the tile it was drawn * into — on a server that has to resample every one, paying for work nobody could * see. *//** How long the scroller must be quiet before held covers are asked for. */export const SETTLE_MS = 90;/** Rows a second past which a scroll is a fling rather than a read. */export const FLING_ROWS_PER_S = 6;/** How long between scroll events before the motion counts as stopped. */const STILL_MS = 50;/** Never ask for more than 2× the tile: beyond that the extra pixels are invisible. */export const COVER_SCALE_CAP = 2;
export interface ScrollPace { /** Feed it the scroller's `scrollTop`, every scroll event. */ onScroll(top: number): void; /** * The pace's own answer, synchronously. * * Rendering reads the signal the `onChange` callback feeds, not this. It exists * because a signal write commits on the microtask queue in Solid 2, so asking * the signal what it just decided, in the same turn, asks it what it decided * last turn — and the pace's own control flow (arm the release only while held, * stop holding when disposed) needs the truth now. An earlier version asked, and * never armed the release, so the gallery held covers for a reader who had long * since stopped. */ isFlinging(): boolean; /** Stop the timer and forget the motion. Component teardown. */ dispose(): void;}
/** * Velocity of the gallery's scroller, reported to whoever is watching. * * Velocity is measured between consecutive scroll events rather than sampled on a * timer: the browser already says when the content moved, and sampling is exactly * how a timer misses the fastest motion. But it is measured only while events are * arriving — a movement whose previous event was more than `STILL_MS` ago is a * reader who had stopped and then moved again, and dividing by that gap reports * any movement at all as standing still (and the very first event of the session, * whose previous timestamp is the epoch, as the fastest thing on the page). * * The threshold is in rows, so the metric matches the thing being moved — six * 208px rows a second is a fling — which is what keeps ordinary reading loading * covers as it goes, and a `PageDown` (one event, after a pause) loading the * screenful it just asked for. * * It reports through `onChange` rather than owning a signal of its own: the pace * is a plain object with no owner, and reactive state created and written from an * unowned scope is the kind of thing that reads back stale in dev and never * disposes. The component that owns the pace owns the signal; the writes land in * the scroll handler and the timer, which are both places Solid expects a write. */export function createScrollPace( rowHeight: number, onChange: (flinging: boolean) => void,): ScrollPace { const pxPerMs = (rowHeight * FLING_ROWS_PER_S) / 1000; let lastTop = 0; // The pace's own birth, not the epoch: an event measured against 1970 is // infinitely slow, which would make the first scroll of every page — the one // that opens the gallery with a flick of the thumb — look like reading. let lastAt = Date.now(); let held = false; let release: ReturnType<typeof setTimeout> | undefined = undefined;
return { onScroll(top: number): void { const at = Date.now(); const dt = at - lastAt; const moved = Math.abs(top - lastTop); lastTop = top; lastAt = at; // Two events in the same millisecond divide by zero, and `Infinity` is the // right answer to "how fast did that move" — a movement the browser itself // reports as instantaneous is the fastest thing here. No movement in no time // is NaN, which is not greater than anything and so is not a fling. if (!held && dt <= STILL_MS && moved / dt > pxPerMs) { held = true; onChange(true); } if (!held) return; // Re-armed on every event: it is the quiet that ends a fling, and the last // event of one is followed by nothing at all. if (release !== undefined) clearTimeout(release); release = setTimeout(() => { release = undefined; if (!held) return; held = false; onChange(false); }, SETTLE_MS); }, isFlinging: () => held, dispose(): void { if (release !== undefined) clearTimeout(release); release = undefined; lastTop = 0; lastAt = 0; if (!held) return; held = false; onChange(false); }, };}
/** * The covers this page has already seen land, by URL. * * Not a cache of pixels — the browser has that, and a second request for one of * these is a memory-cache hit rather than a round trip. It answers a question the * browser will not answer for us: *was this one already here?* Which decides two * things about a cell that comes back into the window: it is not held behind the * queue (it has no queue to be behind), and it does not fade up from initials a * second time — the difference between scrolling back to an album and watching it * remember what it looks like. * * Bounded by insertion order: at five figures of albums the set is the only memory * in the room, and the oldest covers are the least likely to be scrolled back to. */const LANDED_CAP = 4096;const landedUrls = new Set<string>();
export function markCoverLanded(url: string): void { if (!url) return; if (landedUrls.size >= LANDED_CAP) { const oldest = landedUrls.values().next(); if (!oldest.done) landedUrls.delete(oldest.value); } landedUrls.add(url);}
export function coverHasLanded(url: string): boolean { return url !== "" && landedUrls.has(url);}
/** Test seam: a page that has seen one library should not answer for the next. */export function forgetLandedCovers(): void { landedUrls.clear();}
/** * The pixel size to ask `getCoverArt` for, given the tile it will be drawn into. * * 1× when the screen is 1×: the server's resample is the expensive part of a * cover, and producing four times the pixels the display can show buys nothing but * a longer queue. Clamped to 2 so a 3× phone still gets the crispest sleeve the * tile can use, and floored to 1 so a reported ratio below 1 (browser zoom out) * cannot ask for an upscaled blur. */export function coverRequestSize( cellPx: number, dpr: number = typeof devicePixelRatio === "number" ? devicePixelRatio : 1,): number { if (!Number.isFinite(dpr) || dpr <= 0) return cellPx; return Math.round(cellPx * Math.min(COVER_SCALE_CAP, Math.max(1, dpr)));}