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

notifyManager.md

182 lines3.3 KB

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