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

notifyManager.md

93 lines2.6 KB

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```