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

notifyManager.md

185 lines3.3 KB

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