Skip to content

armgit / bygga

Public Imported from GitHub
0

IKEA assembly, step by step: animated, manual-style 3D guides. Scan any paper manual with Together AI.

TypeScript

Render lazy thumbnails only while they are on screen

Youssefe0438fb9933b
diff --git a/src/components/Thumb.tsx b/src/components/Thumb.tsx
index 2577529..8f384a5 100644
--- a/src/components/Thumb.tsx
+++ b/src/components/Thumb.tsx
@@ -3,10 +3,13 @@
import { useEffect, useRef, useState } from "react";
import type { Plan } from "@/lib/types";
import type { StageMode } from "@/engine/stage";
-import { peekThumbnail, thumbnail } from "@/engine/thumbs";
+import { peekThumbnail, requestThumbnail } from "@/engine/thumbs";
import { useMounted } from "@/lib/useMounted";
-/** A static line drawing of a plan, rendered offscreen. With `lazy`, it waits until scrolled into view. */
+/**
+ * A static line drawing of a plan, rendered offscreen. With `lazy`, it renders only while it is on screen
+ * (or about to be): one scrolled past before its turn comes is taken off the render queue.
+ */
export default function Thumb({
id,
plan,
@@ -29,35 +32,28 @@ export default function Thumb({
const key = `${id}:${JSON.stringify(mode)}:${w}x${h}`;
const mounted = useMounted();
const el = useRef<HTMLSpanElement>(null);
- const [seen, setSeen] = useState(!lazy);
+ const [near, setNear] = useState(!lazy);
const [loaded, setLoaded] = useState<{ key: string; url: string } | null>(null);
const url = (mounted ? peekThumbnail(key) : undefined) ?? (loaded?.key === key ? loaded.url : undefined);
useEffect(() => {
- if (seen || !el.current) return;
- const io = new IntersectionObserver(
- (e) => {
- if (e.some((x) => x.isIntersecting)) {
- setSeen(true);
- io.disconnect();
- }
- },
- { rootMargin: "300px" },
- );
+ if (!lazy || !el.current) return;
+ const io = new IntersectionObserver((e) => setNear(e[e.length - 1].isIntersecting), { rootMargin: "200px" });
io.observe(el.current);
return () => io.disconnect();
- }, [seen]);
+ }, [lazy]);
useEffect(() => {
- if (!seen || !plan || peekThumbnail(key)) return;
+ if (!near || !plan || peekThumbnail(key)) return;
let alive = true;
const p = typeof plan === "function" ? plan() : plan;
- thumbnail(key, p, mode, w, h).then((u) => alive && setLoaded({ key, url: u }));
+ const cancel = requestThumbnail(key, p, mode, w, h, (u) => alive && setLoaded({ key, url: u }));
return () => {
alive = false;
+ cancel();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [key, plan, seen]);
+ }, [key, plan, near]);
return (
<span ref={el} className={"thumb " + (className ?? "") + (url ? " ready" : "")} style={{ aspectRatio: `${w} / ${h}` }}>
diff --git a/src/engine/thumbs.ts b/src/engine/thumbs.ts
index a391c94..09415e4 100644
--- a/src/engine/thumbs.ts
+++ b/src/engine/thumbs.ts
@@ -64,6 +64,34 @@ export function thumbnail(key: string, plan: Plan, mode: StageMode, w = 360, h =
return shot(key, plan, mode, w, h).then((s) => s.url);
}
+/**
+ * Like thumbnail, but the returned function takes the request back: a drawing nobody waits for any more
+ * leaves the queue before it renders, so a long grid scrolled past quickly doesn't render every card.
+ */
+export function requestThumbnail(key: string, plan: Plan, mode: StageMode, w: number, h: number, done: (url: string) => void): () => void {
+ const hit = cache.get(key);
+ if (hit) {
+ done(hit.url);
+ return () => {};
+ }
+ const cb = (s: Shot) => done(s.url);
+ const list = waiting.get(key) ?? [];
+ list.push(cb);
+ waiting.set(key, list);
+ if (!queue.some((q) => q.key === key)) queue.push({ key, plan, mode, w, h });
+ pump();
+ return () => {
+ const left = waiting.get(key);
+ if (!left) return;
+ const i = left.indexOf(cb);
+ if (i >= 0) left.splice(i, 1);
+ if (left.length) return;
+ waiting.delete(key);
+ const j = queue.findIndex((q) => q.key === key);
+ if (j >= 0) queue.splice(j, 1);
+ };
+}
+
export function peekThumbnail(key: string) {
return cache.get(key)?.url;
}