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

notifyManager.md

183 lines3.3 KB

1---
2id: notifyManager
3title: notifyManager
4redirect_from:
5 - reference/notifyManager
6 - framework/react/reference/notifyManager
7---
8​
9```ts
10const notifyManager: object;
11```
12​
13Defined in: [packages/query-core/src/notifyManager.ts:154](https://github.com/TanStack/query/blob/main/packages/query-core/src/notifyManager.ts#L154)
14​
15Handles scheduling and batching callbacks in TanStack Query.
16​
17## Type Declaration
18​
19### batch
20​
21```ts
22readonly batch: <T>(callback: () => T) => T;
23```
24​
25Batches all updates scheduled inside the passed callback.
26This is mainly used internally to optimize query client updating.
27Batches can be nested; the queue is only flushed once the outermost `batch` call finishes.
28The return value of `callback` is passed through.
29​
30#### Type Parameters
31​
32##### T
33​
34`T`
35​
36#### Parameters
37​
38##### callback
39​
40() => `T`
41​
42The function to run in the batch.
43​
44#### Returns
45​
46`T`
47​
48The return value of `callback`.
49​
50### batchCalls
51​
52```ts
53readonly batchCalls: <T>(callback: BatchCallsCallback<T>) => BatchCallsCallback<T>;
54```
55​
56All calls to the wrapped function will be batched.
57​
58#### Type Parameters
59​
60##### T
61​
62`T` *extends* `unknown`[]
63​
64#### Parameters
65​
66##### callback
67​
68`BatchCallsCallback`\<`T`\>
69​
70The function to wrap.
71​
72#### Returns
73​
74`BatchCallsCallback`\<`T`\>
75​
76A function that schedules a call to `callback` with the given arguments.
77​
78### schedule
79​
80```ts
81schedule: (callback: NotifyCallback) => void;
82```
83​
84Schedules a function to be run on the next batch.
85By default, the batch is run with a `setTimeout`, but this can be configured via `setScheduler`.
86​
87#### Parameters
88​
89##### callback
90​
91`NotifyCallback`
92​
93#### Returns
94​
95`void`
96​
97### setBatchNotifyFunction
98​
99```ts
100readonly setBatchNotifyFunction: (fn: BatchNotifyFunction) => void;
101```
102​
103Use this method to set a custom function to batch notifications together into a single tick.
104Framework adapters use this to plug in their own batching primitive, so that a single query
105update only triggers one re-render instead of one per subscriber.
106​
107#### Parameters
108​
109##### fn
110​
111`BatchNotifyFunction`
112​
113Receives a function that runs a batch of notifications and must call it.
114​
115#### Returns
116​
117`void`
118​
119#### Example
120​
121```ts
122import { notifyManager } from '@tanstack/query-core'
123import { batch } from 'solid-js'
124​
125notifyManager.setBatchNotifyFunction(batch)
126```
127​
128### setNotifyFunction
129​
130```ts
131readonly setNotifyFunction: (fn: NotifyFunction) => void;
132```
133​
134Use this method to set a custom notify function.
135This can be used to for example wrap notifications with `React.act` while running tests.
136​
137#### Parameters
138​
139##### fn
140​
141`NotifyFunction`
142​
143Receives each notification callback and must call it.
144​
145#### Returns
146​
147`void`
148​
149### setScheduler
150​
151```ts
152readonly setScheduler: (fn: ScheduleFunction) => void;
153```
154​
155Configures a custom callback that schedules when the next batch runs.
156The default behavior is `setTimeout(callback, 0)`.
157​
158#### Parameters
159​
160##### fn
161​
162`ScheduleFunction`
163​
164Receives a callback that runs the next batch, and schedules it.
165​
166#### Returns
167​
168`void`
169​
170#### Example
171​
172```ts
173import { notifyManager } from '@tanstack/query-core'
174​
175// Schedule batches in the next microtask
176notifyManager.setScheduler(queueMicrotask)
177​
178// Schedule batches before the next frame is rendered
179notifyManager.setScheduler(requestAnimationFrame)
180​
181// Schedule batches some time in the future
182notifyManager.setScheduler((cb) => setTimeout(cb, 10))
183```