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

notifyManager.md

166 lines2.8 KB

1---
2id: notifyManager
3title: notifyManager
4---
5​
6```ts
7const notifyManager: object;
8```
9​
10Defined in: [packages/query-core/src/notifyManager.ts:144](https://github.com/TanStack/query/blob/main/packages/query-core/src/notifyManager.ts#L144)
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​
39#### Returns
40​
41`T`
42​
43### batchCalls()
44​
45```ts
46readonly batchCalls: <T>(callback: BatchCallsCallback<T>) => BatchCallsCallback<T>;
47```
48​
49All calls to the wrapped function will be batched.
50​
51#### Type Parameters
52​
53##### T
54​
55`T` *extends* `unknown`[]
56​
57#### Parameters
58​
59##### callback
60​
61`BatchCallsCallback`\<`T`\>
62​
63#### Returns
64​
65`BatchCallsCallback`\<`T`\>
66​
67### schedule()
68​
69```ts
70schedule: (callback: NotifyCallback) => void;
71```
72​
73Schedules a function to be run on the next batch.
74By default, the batch is run with a `setTimeout`, but this can be configured via `setScheduler`.
75​
76#### Parameters
77​
78##### callback
79​
80`NotifyCallback`
81​
82#### Returns
83​
84`void`
85​
86### setBatchNotifyFunction()
87​
88```ts
89readonly setBatchNotifyFunction: (fn: BatchNotifyFunction) => void;
90```
91​
92Use this method to set a custom function to batch notifications together into a single tick.
93Framework adapters use this to plug in their own batching primitive, so that a single query
94update only triggers one re-render instead of one per subscriber.
95​
96#### Parameters
97​
98##### fn
99​
100`BatchNotifyFunction`
101​
102#### Returns
103​
104`void`
105​
106#### Example
107​
108```ts
109import { notifyManager } from '@tanstack/query-core'
110import { batch } from 'solid-js'
111​
112notifyManager.setBatchNotifyFunction(batch)
113```
114​
115### setNotifyFunction()
116​
117```ts
118readonly setNotifyFunction: (fn: NotifyFunction) => void;
119```
120​
121Use this method to set a custom notify function.
122This can be used to for example wrap notifications with `React.act` while running tests.
123​
124#### Parameters
125​
126##### fn
127​
128`NotifyFunction`
129​
130#### Returns
131​
132`void`
133​
134### setScheduler()
135​
136```ts
137readonly setScheduler: (fn: ScheduleFunction) => void;
138```
139​
140Configures a custom callback that schedules when the next batch runs.
141The default behavior is `setTimeout(callback, 0)`.
142​
143#### Parameters
144​
145##### fn
146​
147`ScheduleFunction`
148​
149#### Returns
150​
151`void`
152​
153#### Example
154​
155```ts
156import { notifyManager } from '@tanstack/query-core'
157​
158// Schedule batches in the next microtask
159notifyManager.setScheduler(queueMicrotask)
160​
161// Schedule batches before the next frame is rendered
162notifyManager.setScheduler(requestAnimationFrame)
163​
164// Schedule batches some time in the future
165notifyManager.setScheduler((cb) => setTimeout(cb, 10))
166```