Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-query): suspense for useQueries (#4498)
* feat: suspense for useQueries * generics for fetchOptimistic * tests: useQueries suspense
- Loading branch information
Showing
5 changed files
with
219 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { DefaultedQueryObserverOptions } from '@tanstack/query-core' | ||
import type { QueryObserver } from '@tanstack/query-core' | ||
import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary' | ||
import type { QueryObserverResult } from '@tanstack/query-core' | ||
import type { QueryKey } from '@tanstack/query-core' | ||
|
||
export const ensureStaleTime = ( | ||
defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>, | ||
) => { | ||
if (defaultedOptions.suspense) { | ||
// Always set stale time when using suspense to prevent | ||
// fetching again when directly mounting after suspending | ||
if (typeof defaultedOptions.staleTime !== 'number') { | ||
defaultedOptions.staleTime = 1000 | ||
} | ||
} | ||
} | ||
|
||
export const willFetch = ( | ||
result: QueryObserverResult<any, any>, | ||
isRestoring: boolean, | ||
) => result.isLoading && result.isFetching && !isRestoring | ||
|
||
export const shouldSuspend = ( | ||
defaultedOptions: | ||
| DefaultedQueryObserverOptions<any, any, any, any, any> | ||
| undefined, | ||
result: QueryObserverResult<any, any>, | ||
isRestoring: boolean, | ||
) => defaultedOptions?.suspense && willFetch(result, isRestoring) | ||
|
||
export const fetchOptimistic = < | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey extends QueryKey, | ||
>( | ||
defaultedOptions: DefaultedQueryObserverOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey | ||
>, | ||
observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>, | ||
errorResetBoundary: QueryErrorResetBoundaryValue, | ||
) => | ||
observer | ||
.fetchOptimistic(defaultedOptions) | ||
.then(({ data }) => { | ||
defaultedOptions.onSuccess?.(data as TData) | ||
defaultedOptions.onSettled?.(data, null) | ||
}) | ||
.catch((error) => { | ||
errorResetBoundary.clearReset() | ||
defaultedOptions.onError?.(error) | ||
defaultedOptions.onSettled?.(undefined, error) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters