The notifyManager
handles scheduling and batching callbacks in Tanstack Query.
It exposes the following methods:
notifyManager.batch
#
batch
can be used to batch all updates scheduled inside the passed callback.
This is mainly used internally to optimize queryClient updating.
1function batch<T>(callback: () => T): T
notifyManager.batchCalls
#
batchCalls
is a higher-order function that takes a callback and wraps it.
All calls to the wrapped function schedule the callback to be run on the next batch.
1type BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void
2
3function batchCalls<T extends Array<unknown>>(
4 callback: BatchCallsCallback<T>,
5): BatchCallsCallback<T>
notifyManager.schedule
#
schedule
schedules a function to be run on the next batch. By default, the batch is run
with a setTimeout, but this can be configured.
1function schedule(callback: () => void): void
notifyManager.setNotifyFunction
#
setNotifyFunction
overrides the notify function. This function is passed the
callback when it should be executed. The default notifyFunction just calls it.
This can be used to for example wrap notifications with React.act
while running tests:
1import { notifyManager } from '@tanstack/react-query'
2import { act } from 'react-dom/test-utils'
3
4notifyManager.setNotifyFunction(act)
notifyManager.setBatchNotifyFunction
#
setBatchNotifyFunction
sets the function to use for batched updates
If your framework supports a custom batching function, you can let TanStack Query know about it by calling notifyManager.setBatchNotifyFunction.
For example, this is how the batch function is set in solid-query:
1import { notifyManager } from '@tanstack/query-core'
2import { batch } from 'solid-js'
3
4notifyManager.setBatchNotifyFunction(batch)
notifyManager.setScheduler
#
setScheduler
configures a custom callback that should schedules when the next
batch runs. The default behaviour is setTimeout(callback, 0)
.
1import { notifyManager } from '@tanstack/react-query'
2
3// Schedule batches in the next microtask
4notifyManager.setScheduler(queueMicrotask)
5
6// Schedule batches before the next frame is rendered
7notifyManager.setScheduler(requestAnimationFrame)
8
9// Schedule batches some time in the future
10notifyManager.setScheduler((cb) => setTimeout(cb, 10))