Open History to see the last commit
1// TYPES
2β
3import { systemSetTimeoutZero } from './timeoutManager'
4β
5type NotifyCallback = () => void
6β
7type NotifyFunction = (callback: () => void) => void
8β
9type BatchNotifyFunction = (callback: () => void) => void
10β
11type BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void
12β
13type ScheduleFunction = (callback: () => void) => void
14β
15/**
16 * Default scheduling function used by the notify manager.
17 * Schedules the callback with the system's `setTimeout(callback, 0)`.
18 */
19export const defaultScheduler: ScheduleFunction = systemSetTimeoutZero
20β
21export function createNotifyManager() {
22 let queue: Array<NotifyCallback> = []
23 let transactions = 0
24 let notifyFn: NotifyFunction = (callback) => {
25 callback()
26 }
27 let batchNotifyFn: BatchNotifyFunction = (callback: () => void) => {
28 callback()
29 }
30 let scheduleFn = defaultScheduler
31β
32 const schedule = (callback: NotifyCallback): void => {
33 if (transactions) {
34 queue.push(callback)
35 } else {
36 scheduleFn(() => {
37 notifyFn(callback)
38 })
39 }
40 }
41 const flush = (): void => {
42 const originalQueue = queue
43 queue = []
44 if (originalQueue.length) {
45 scheduleFn(() => {
46 batchNotifyFn(() => {
47 originalQueue.forEach((callback) => {
48 notifyFn(callback)
49 })
50 })
51 })
52 }
53 }
54β
55 return {
56 /**
57 * Batches all updates scheduled inside the passed callback.
58 * This is mainly used internally to optimize query client updating.
59 * Batches can be nested; the queue is only flushed once the outermost `batch` call finishes.
60 * The return value of `callback` is passed through.
61 */
62 batch: <T>(callback: () => T): T => {
63 let result
64 transactions++
65 try {
66 result = callback()
67 } finally {
68 transactions--
69 if (!transactions) {
70 flush()
71 }
72 }
73 return result
74 },
75 /**
76 * All calls to the wrapped function will be batched.
77 */
78 batchCalls: <T extends Array<unknown>>(
79 callback: BatchCallsCallback<T>,
80 ): BatchCallsCallback<T> => {
81 return (...args) => {
82 schedule(() => {
83 callback(...args)
84 })
85 }
86 },
87 /**
88 * Schedules a function to be run on the next batch.
89 * By default, the batch is run with a `setTimeout`, but this can be configured via `setScheduler`.
90 */
91 schedule,
92 /**
93 * Use this method to set a custom notify function.
94 * This can be used to for example wrap notifications with `React.act` while running tests.
95 */
96 setNotifyFunction: (fn: NotifyFunction) => {
97 notifyFn = fn
98 },
99 /**
100 * Use this method to set a custom function to batch notifications together into a single tick.
101 * Framework adapters use this to plug in their own batching primitive, so that a single query
102 * update only triggers one re-render instead of one per subscriber.
103 *
104 * @example
105 * ```ts
106 * import { notifyManager } from '@tanstack/query-core'
107 * import { batch } from 'solid-js'
108 *
109 * notifyManager.setBatchNotifyFunction(batch)
110 * ```
111 */
112 setBatchNotifyFunction: (fn: BatchNotifyFunction) => {
113 batchNotifyFn = fn
114 },
115 /**
116 * Configures a custom callback that schedules when the next batch runs.
117 * The default behavior is `setTimeout(callback, 0)`.
118 *
119 * @example
120 * ```ts
121 * import { notifyManager } from '@tanstack/query-core'
122 *
123 * // Schedule batches in the next microtask
124 * notifyManager.setScheduler(queueMicrotask)
125 *
126 * // Schedule batches before the next frame is rendered
127 * notifyManager.setScheduler(requestAnimationFrame)
128 *
129 * // Schedule batches some time in the future
130 * notifyManager.setScheduler((cb) => setTimeout(cb, 10))
131 * ```
132 */
133 setScheduler: (fn: ScheduleFunction) => {
134 scheduleFn = fn
135 },
136 } as const
137}
138β
139// SINGLETON
140β
141/**
142 * Handles scheduling and batching callbacks in TanStack Query.
143 */
144export const notifyManager = createNotifyManager()