v3: the tests learn to expect trouble, and seven bugs they now catch are fixed — a review of the two test-suite commits found them written mostly for the happy path; each fix below comes with the test that fails without it. Infinite scroll: a later page that failed turned loadingMore back off, which made a new IntersectionObserver, which reports a sentinel still in view at once — so an offline feed asked again as fast as the request could fail, on Search, Index, the followed feeds, user dumps and user playlists alike. useInfiniteScroll now waits 1s, 2s, 4s… up to 30s while the sentinel stays put, counted from the last load so re-renders don't restart it, and asks at once again when the sentinel leaves the view or is pushed down by new items; the fake observer in src/test can report an element in view on observe(), as a browser's does. Playlist covers: the row was updated and playlist_updated sent before the file was written, so a client fetching image?v=<new> in between cached the old cover (or a 404) under the new URL for an hour, and a failed write left image_mime set with no file behind it. The file now goes in first, written beside and renamed over, and only then does the row change. Playlist order: the server wanted every member named, but GET hides private dumps the requester can't see, so a playlist holding someone else's dump that went private — or one a moderator reorders around the owner's private dumps — could never be reordered. The requester now orders what they can see and hidden members keep their slots; the visibility rule lives in api/lib/playlist-members.ts, shared by GET, reorder and the socket. playlist_dumps_updated also sent every member's id, private ones included, to a public playlist's whole audience; each viewer now gets the ids they can see, plus a dumpCount of all members so playlist cards keep the server's count. File dumps: replacing the file and changing visibility took two requests, so a failed second one left the new file live under the old visibility; PUT /api/dumps/:id/file takes isPrivate and applies both in one UPDATE, and it now broadcasts like any edit (it sent nothing, so other viewers never saw a new title). Notifications: one that arrived while the list was loading was dropped, then marked read by the read-all after the load, so it never showed; it's kept and merged in, once. Ranges: bytes=0-0,5-6 got a 416; several ranges, another unit or an unparseable header are ignored now and the whole file sent with a 200, as RFC 9110 allows — the old test asserted the 416.
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 49s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 49s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
/** Longest wait between two loads while the sentinel never left the view. */
|
||||
const MAX_RETRY_DELAY_MS = 30_000;
|
||||
|
||||
/**
|
||||
* Observe a sentinel element and call `onLoadMore` when it scrolls into view.
|
||||
@@ -8,26 +11,69 @@ import { useCallback, useEffect, useState } from "react";
|
||||
* changes — e.g. when the index page swaps the active feed tab and a fresh
|
||||
* sentinel `<div>` mounts in place of the old one. A static ref would keep
|
||||
* observing the now-detached node and silently stop loading more.
|
||||
*
|
||||
* Each change of `enabled` makes a new observer, which reports the sentinel
|
||||
* at once if it's still in view. After a load that added items that's what
|
||||
* fills a tall screen; after one that failed it would ask again straight
|
||||
* away, and again, as fast as the request can fail. So a sentinel that stays
|
||||
* put in view waits longer each time — 1s, 2s, 4s… up to 30s — and goes back
|
||||
* to asking at once when it leaves the view or is pushed down by new items.
|
||||
*/
|
||||
export function useInfiniteScroll(onLoadMore: () => void, enabled: boolean) {
|
||||
const [sentinel, setSentinel] = useState<HTMLDivElement | null>(null);
|
||||
// Loads asked for since the sentinel last left the view or moved.
|
||||
const attemptsRef = useRef(0);
|
||||
const lastLoadAtRef = useRef(0);
|
||||
const lastTopRef = useRef<number | null>(null);
|
||||
|
||||
const handleIntersect = useCallback(
|
||||
(entries: IntersectionObserverEntry[]) => {
|
||||
if (entries[0].isIntersecting && enabled) {
|
||||
onLoadMore();
|
||||
(entries: IntersectionObserverEntry[]): (() => void) | undefined => {
|
||||
const entry = entries[0];
|
||||
const top = entry.boundingClientRect?.top ?? null;
|
||||
if (!entry.isIntersecting) {
|
||||
attemptsRef.current = 0;
|
||||
lastTopRef.current = null;
|
||||
return;
|
||||
}
|
||||
if (
|
||||
top !== null && lastTopRef.current !== null && top > lastTopRef.current
|
||||
) {
|
||||
attemptsRef.current = 0;
|
||||
}
|
||||
lastTopRef.current = top;
|
||||
if (!enabled) return;
|
||||
const attempts = attemptsRef.current;
|
||||
const load = () => {
|
||||
attemptsRef.current = attempts + 1;
|
||||
lastLoadAtRef.current = Date.now();
|
||||
onLoadMore();
|
||||
};
|
||||
// Counted from the last load, not from now: the observer is remade on
|
||||
// every render that changes onLoadMore, which mustn't restart the wait.
|
||||
const wait = attempts === 0 ? 0 : lastLoadAtRef.current +
|
||||
Math.min(1000 * 2 ** (attempts - 1), MAX_RETRY_DELAY_MS) - Date.now();
|
||||
if (wait <= 0) {
|
||||
load();
|
||||
return;
|
||||
}
|
||||
const timer = setTimeout(load, wait);
|
||||
return () => clearTimeout(timer);
|
||||
},
|
||||
[onLoadMore, enabled],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!sentinel) return;
|
||||
const observer = new IntersectionObserver(handleIntersect, {
|
||||
rootMargin: "200px",
|
||||
});
|
||||
let cancelPending: (() => void) | undefined;
|
||||
const observer = new IntersectionObserver((entries) => {
|
||||
cancelPending?.();
|
||||
cancelPending = handleIntersect(entries);
|
||||
}, { rootMargin: "200px" });
|
||||
observer.observe(sentinel);
|
||||
return () => observer.disconnect();
|
||||
return () => {
|
||||
cancelPending?.();
|
||||
observer.disconnect();
|
||||
};
|
||||
}, [handleIntersect, sentinel]);
|
||||
|
||||
return setSentinel;
|
||||
|
||||
@@ -87,7 +87,10 @@ export function usePlaylistListSync(
|
||||
const idx = prev.findIndex((p) => p.id === ev.playlistId);
|
||||
if (idx === -1) return prev;
|
||||
const next = [...prev];
|
||||
next[idx] = { ...next[idx], dumpCount: ev.dumpIds.length };
|
||||
next[idx] = {
|
||||
...next[idx],
|
||||
dumpCount: ev.dumpCount ?? ev.dumpIds.length,
|
||||
};
|
||||
return next;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user