Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

notifyManager.md

95 lines2.7 KB

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