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

notifyManager.md

180 lines3.2 KB

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