Open History to see the last commit
1---
2id: NotifyManager
3title: NotifyManager
4---
5β
6The `notifyManager` handles scheduling and batching callbacks in Tanstack Query.
7β
8It exposes the following methods:
9β
10- [batch](#notifymanagerbatch)
11- [batchCalls](#notifymanagerbatchcalls)
12- [schedule](#notifymanagerschedule)
13- [setNotifyFunction](#notifymanagersetnotifyfunction)
14- [setBatchNotifyFunction](#notifymanagersetbatchnotifyfunction)
15- [setScheduler](#notifymanagersetscheduler)
16β
17## `notifyManager.batch`
18β
19`batch` can be used to batch all updates scheduled inside the passed callback.
20This is mainly used internally to optimize queryClient updating.
21β
22```ts
23function batch<T>(callback: () => T): T
24```
25β
26## `notifyManager.batchCalls`
27β
28`batchCalls` is a higher-order function that takes a callback and wraps it.
29All calls to the wrapped function schedule the callback to be run on the next batch.
30β
31```ts
32type BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void
33β
34function batchCalls<T extends Array<unknown>>(
35 callback: BatchCallsCallback<T>,
36): BatchCallsCallback<T>
37```
38β
39## `notifyManager.schedule`
40β
41`schedule` schedules a function to be run on the next batch. By default, the batch is run
42with a setTimeout, but this can be configured.
43β
44```ts
45function schedule(callback: () => void): void
46```
47β
48## `notifyManager.setNotifyFunction`
49β
50`setNotifyFunction` overrides the notify function. This function is passed the
51callback when it should be executed. The default notifyFunction just calls it.
52β
53This can be used to for example wrap notifications with `React.act` while running tests:
54β
55```ts
56import { notifyManager } from '@tanstack/react-query'
57import { act } from 'react-dom/test-utils'
58β
59notifyManager.setNotifyFunction(act)
60```
61β
62## `notifyManager.setBatchNotifyFunction`
63β
64`setBatchNotifyFunction` sets the function to use for batched updates
65β
66If your framework supports a custom batching function, you can let TanStack Query know about it by calling notifyManager.setBatchNotifyFunction.
67β
68For example, this is how the batch function is set in solid-query:
69β
70```ts
71import { notifyManager } from '@tanstack/query-core'
72import { batch } from 'solid-js'
73β
74notifyManager.setBatchNotifyFunction(batch)
75```
76β
77## `notifyManager.setScheduler`
78β
79`setScheduler` configures a custom callback that should schedules when the next
80batch runs. The default behaviour is `setTimeout(callback, 0)`.
81β
82```ts
83import { notifyManager } from '@tanstack/react-query'
84β
85// Schedule batches in the next microtask
86notifyManager.setScheduler(queueMicrotask)
87β
88// Schedule batches before the next frame is rendered
89notifyManager.setScheduler(requestAnimationFrame)
90β
91// Schedule batches some time in the future
92notifyManager.setScheduler((cb) => setTimeout(cb, 10))
93```