notifyManager
1const notifyManager: object;定义位置:packages/query-core/src/notifyManager.ts:154
负责调度和批处理 TanStack Query 中的回调函数。
类型声明
batch
1readonly batch: <T>(callback: () => T) => T;对传入回调函数内计划执行的所有更新进行批处理。
该方法主要在内部用于优化查询客户端(QueryClient)的更新。
批处理可嵌套;仅当最外层的 batch 调用结束后,队列才会被清空。
callback 的返回值会被透传。
类型参数
T
T
参数
callback
() => T
需在批处理中运行的函数。
返回值
T
callback 的返回值。
batchCalls
1readonly batchCalls: <T>(callback: BatchCallsCallback<T>) => BatchCallsCallback<T>;对所有调用包装后函数的操作进行批处理。
类型参数
T
T extends unknown[]
参数
callback
BatchCallsCallback<T>
待包装的函数。
返回值
BatchCallsCallback<T>
一个函数,它会以给定参数调度一次对 callback 的调用。
schedule
1schedule: (callback: NotifyCallback) => void;将一个函数安排在下一个批处理周期中运行。
默认情况下,批处理使用 setTimeout 执行,但可通过 setScheduler 进行配置。
参数
callback
NotifyCallback
返回值
void
setBatchNotifyFunction
1readonly setBatchNotifyFunction: (fn: BatchNotifyFunction) => void;使用此方法设置一个自定义函数,将通知合并为单次事件循环(tick)。 框架适配器通过该方法接入其自身的批处理原语,从而确保单个查询更新仅触发一次重新渲染(re-render),而非每个订阅者(subscriber)各触发一次。
参数
fn
BatchNotifyFunction
接收一个执行通知批处理的函数,并必须调用它。
返回值
void
示例
1import { notifyManager } from '@tanstack/query-core'2import { batch } from 'solid-js'3 4notifyManager.setBatchNotifyFunction(batch)setNotifyFunction
1readonly setNotifyFunction: (fn: NotifyFunction) => void;使用此方法设置一个自定义通知函数。
例如,在测试中可利用该方法将通知包裹在 React.act 中。
参数
fn
NotifyFunction
接收每个通知回调,并必须调用它。
返回值
void
setScheduler
1readonly setScheduler: (fn: ScheduleFunction) => void;配置一个自定义回调函数,用于决定下一批处理何时运行。
默认行为是 setTimeout(callback, 0)。
参数
fn
ScheduleFunction
接收一个执行下一批处理的回调,并负责调度其运行。
返回值
void
示例
1import { notifyManager } from '@tanstack/query-core'2 3// Schedule batches in the next microtask4notifyManager.setScheduler(queueMicrotask)5 6// Schedule batches before the next frame is rendered7notifyManager.setScheduler(requestAnimationFrame)8 9// Schedule batches some time in the future10notifyManager.setScheduler((cb) => setTimeout(cb, 10))