Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/await on success #5473

Merged
merged 3 commits into from
May 26, 2023
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
3 changes: 2 additions & 1 deletion docs/react/plugins/persistQueryClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,11 @@ ReactDOM.createRoot(rootElement).render(

- `persistOptions: PersistQueryClientOptions`
- all [options](#options) you can pass to [persistQueryClient](#persistqueryclient) minus the QueryClient itself
- `onSuccess?: () => void`
- `onSuccess?: () => Promise<unknown> | unknown`
- optional
- will be called when the initial restore is finished
- can be used to [resumePausedMutations](../reference/QueryClient#queryclientresumepausedmutations)
- if a Promise is returned, it will be awaited; restoring is seen as ongoing until then

### useIsRestoring

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { QueryClientProvider, IsRestoringProvider } from '@tanstack/react-query'

export type PersistQueryClientProviderProps = QueryClientProviderProps & {
persistOptions: Omit<PersistQueryClientOptions, 'queryClient'>
onSuccess?: () => void
onSuccess?: () => Promise<unknown> | unknown
}

export const PersistQueryClientProvider = ({
Expand All @@ -33,10 +33,13 @@ export const PersistQueryClientProvider = ({
queryClient: client,
})

promise.then(() => {
promise.then(async () => {
if (!isStale) {
refs.current.onSuccess?.()
setIsRestoring(false)
try {
await refs.current.onSuccess?.()
} finally {
setIsRestoring(false)
}
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,66 @@ describe('PersistQueryClientProvider', () => {
await waitFor(() => rendered.getByText('fetched'))
})

test('should await onSuccess after successful restoring', async () => {
const key = queryKey()

const queryClient = createQueryClient()
await queryClient.prefetchQuery({
queryKey: key,
queryFn: () => Promise.resolve('hydrated'),
})

const persister = createMockPersister()

await persistQueryClientSave({ queryClient, persister })

queryClient.clear()

const states: Array<string> = []

function Page() {
const { data, fetchStatus } = useQuery({
queryKey: key,
queryFn: async () => {
states.push('fetching')
await sleep(10)
states.push('fetched')
return 'fetched'
},
})

return (
<div>
<h1>{data}</h1>
<h2>fetchStatus: {fetchStatus}</h2>
</div>
)
}

const rendered = render(
<PersistQueryClientProvider
client={queryClient}
persistOptions={{ persister }}
onSuccess={async () => {
states.push('onSuccess')
await sleep(20)
states.push('onSuccess done')
}}
>
<Page />
</PersistQueryClientProvider>,
)

await waitFor(() => rendered.getByText('hydrated'))
await waitFor(() => rendered.getByText('fetched'))
expect(states).toEqual([
'onSuccess',
'onSuccess done',
'fetching',
'fetched',
])
})

test('should remove cache after non-successful restoring', async () => {
const key = queryKey()
const consoleMock = vi.spyOn(console, 'error')
Expand Down