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))