app: useLatestRequest reports loading before the debounce (#725)
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Controller Tests / controller-tests (push) Waiting to run
Jest Tests / jest-tests (push) Waiting to run

`loading` only became true once the debounce timer fired, so for the
first 300-500ms after every sendRequest the hook reported
data=null, loading=false.  Consumers branch on `loading` to choose
between a spinner and an empty state, so the global search dropdown,
the Zim search tab, and the map search rendered "No results" (or
"No Zims have been indexed") for a search that had not run yet.

Invariants now enforced and tested:
* loading is true from sendRequest until the latest request settles.
* The latest-request id is claimed synchronously in sendRequest, so a
  fetch already in flight is stale the moment a newer request is sent
  and cannot write data or clear loading during the new debounce.
* A stale request that rejects late no longer nulls newer data.
* A pending debounce is cancelled on unmount, and any in-flight fetch
  is treated as stale.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-09-25 14:05:08 -06:00 • committed by GitHub
parent bcd12c9b96
commit 25e00f75fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 268 additions and 3 deletions

View file

@ -195,6 +195,13 @@ export const useRecurringTimeout = (callback, delay) => {
export const useLatestRequest = (delay = 300, defaultLoading = false) => {
// A hook which ignores older requests and will only set `data` to the latest response's data.
// usage: sendRequest(async () => await yourAPICall(...args));
//
// Invariants:
// * `loading` is true from the moment `sendRequest` is called until the latest request settles,
// including the debounce delay. Consumers use `loading` to choose between a spinner and an
// empty state, so it must never be false while a request is pending.
// * Only the latest request may write `data` or clear `loading`. A stale request that settles
// late (resolve or reject) is ignored.
// The results from awaiting `fetchFunction`.
const [data, setData] = React.useState(null);
@ -207,10 +214,16 @@ export const useLatestRequest = (delay = 300, defaultLoading = false) => {
if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current);
}
// Claim the latest id now, not when the debounce fires, so an earlier fetch that is still
// in flight is already stale and cannot write `data` or clear `loading` while this
// request is pending.
const requestId = ++latestRequestRef.current;
// Report the pending request before the debounce so the caller never shows "no results"
// for a search that has not run yet.
setLoading(true);
debounceTimerRef.current = setTimeout(async () => {
setLoading(true);
const requestId = ++latestRequestRef.current;
debounceTimerRef.current = null;
try {
const result = await fetchFunction();
@ -220,7 +233,9 @@ export const useLatestRequest = (delay = 300, defaultLoading = false) => {
}
} catch (error) {
console.error('Request failed:', error);
setData(null);
if (requestId === latestRequestRef.current) {
setData(null);
}
} finally {
if (requestId === latestRequestRef.current) {
setLoading(false);
@ -229,6 +244,18 @@ export const useLatestRequest = (delay = 300, defaultLoading = false) => {
}, delay);
}, [delay]);
// Do not fire a debounced request after the caller has unmounted, and treat any fetch still in
// flight as stale so it does not set state on the unmounted hook.
React.useEffect(() => {
return () => {
if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current);
debounceTimerRef.current = null;
}
latestRequestRef.current++;
};
}, []);
return {data, sendRequest, loading};
};