mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 11:26:25 -05:00
app: useLatestRequest reports loading before the debounce (#725)
`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:
parent
bcd12c9b96
commit
25e00f75fd
2 changed files with 268 additions and 3 deletions
|
|
@ -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};
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue