File size: 2,707 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
---
id: NotifyManager
title: NotifyManager
---
The `notifyManager` handles scheduling and batching callbacks in Tanstack Query.
It exposes the following methods:
- [batch](#notifymanagerbatch)
- [batchCalls](#notifymanagerbatchcalls)
- [schedule](#notifymanagerschedule)
- [setNotifyFunction](#notifymanagersetnotifyfunction)
- [setBatchNotifyFunction](#notifymanagersetbatchnotifyfunction)
- [setScheduler](#notifymanagersetscheduler)
## `notifyManager.batch`
`batch` can be used to batch all updates scheduled inside the passed callback.
This is mainly used internally to optimize queryClient updating.
```ts
function 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.
```ts
type BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void
function batchCalls<T extends Array<unknown>>(
callback: BatchCallsCallback<T>,
): 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.
```ts
function 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:
```ts
import { notifyManager } from '@tanstack/react-query'
import { act } from 'react-dom/test-utils'
notifyManager.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:
```ts
import { notifyManager } from '@tanstack/query-core'
import { batch } from 'solid-js'
notifyManager.setBatchNotifyFunction(batch)
```
## `notifyManager.setScheduler`
`setScheduler` configures a custom callback that should schedules when the next
batch runs. The default behaviour is `setTimeout(callback, 0)`.
```ts
import { notifyManager } from '@tanstack/react-query'
// Schedule batches in the next microtask
notifyManager.setScheduler(queueMicrotask)
// Schedule batches before the next frame is rendered
notifyManager.setScheduler(requestAnimationFrame)
// Schedule batches some time in the future
notifyManager.setScheduler((cb) => setTimeout(cb, 10))
```
|