Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/use-loading-overlapping-calls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-simplikit': patch
---

`useLoading` now keeps `loading` as `true` until every promise passed to `startLoading` has settled. Before, when two calls overlapped, the first promise to settle set `loading` to `false` while the other was still pending.
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,72 @@ describe('useLoading', () => {
// No errors should occur, and the loading state should not update after unmount
expect(true).toBe(true);
});
it('should stay loading until every overlapping promise has settled', async () => {
const { result } = await renderHookSSR(() => useLoading());
const [, startLoading] = result.current;

let resolveFirst: () => void = () => {};
let resolveSecond: () => void = () => {};
const first = new Promise<void>(resolve => {
resolveFirst = resolve;
});
const second = new Promise<void>(resolve => {
resolveSecond = resolve;
});

await act(async () => {
startLoading(first);
startLoading(second);
});

expect(result.current[0]).toBe(true);

await act(async () => {
resolveFirst();
await first;
});

expect(result.current[0]).toBe(true);

await act(async () => {
resolveSecond();
await second;
});

expect(result.current[0]).toBe(false);
});

it('should stay loading while another promise is pending after one rejects', async () => {
const { result } = await renderHookSSR(() => useLoading());
const [, startLoading] = result.current;

let rejectFirst: (error: Error) => void = () => {};
let resolveSecond: () => void = () => {};
const first = new Promise<void>((_, reject) => {
rejectFirst = reject;
});
const second = new Promise<void>(resolve => {
resolveSecond = resolve;
});

let firstResult: Promise<void> = Promise.resolve();
await act(async () => {
firstResult = startLoading(first);
startLoading(second);
});

await act(async () => {
rejectFirst(new Error('failed'));
await expect(firstResult).rejects.toThrow('failed');
});

expect(result.current[0]).toBe(true);

await act(async () => {
resolveSecond();
await second;
});

expect(result.current[0]).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
export function useLoading(): [boolean, <T>(promise: Promise<T>) => Promise<T>] {
const [loading, setLoading] = useState(false);
const ref = useIsMountedRef();
// Counts the promises that have not settled yet, so that `loading` stays `true`
// until the last of several overlapping calls finishes.
const pendingCountRef = useRef(0);

const startTransition = useCallback(
async <T>(promise: Promise<T>) => {
pendingCountRef.current += 1;
try {
setLoading(true);
const data = await promise;
return data;
} finally {
if (ref.isMounted) {
pendingCountRef.current -= 1;
if (ref.isMounted && pendingCountRef.current === 0) {
setLoading(false);
}
}
Expand Down
Loading