NotifyManager
Manages batching and scheduling of notifications in TanStack Query. This utility ensures that multiple state updates are efficiently batched together and scheduled for execution, improving performance by reducing unnecessary re-renders.Instance
A singleton instance is exported asnotifyManager:
Methods
batch
Executes a callback function within a batch context, deferring all notifications until the batch completes.() => T
required
The function to execute within the batch context. All notifications triggered during this function’s execution will be queued and flushed together when the batch completes.
- Increments a transaction counter before executing the callback
- Queues all scheduled notifications during callback execution
- Decrements the transaction counter after the callback completes
- Flushes all queued notifications when the transaction counter reaches zero
- Supports nested batches (transactions stack)
- The callback’s return value is preserved and returned
schedule
Schedules a callback to be executed, either immediately or after the current batch completes.() => void
required
The callback function to schedule for execution.
- If currently in a batch (transaction count > 0), adds the callback to the queue
- If not in a batch, schedules the callback to run asynchronously using the scheduler function
- By default, uses
setTimeoutwith 0 delay for scheduling - Wraps the callback with the configured notify function
batchCalls
Wraps a callback function so that all calls to it are automatically batched.(...args: T) => void
required
The callback function to wrap. The wrapped function will have the same signature but all invocations will be batched.
- Returns a wrapped function that schedules the original callback instead of calling it directly
- Each call to the wrapped function is scheduled individually but may be batched with others
- Preserves the original function’s arguments
- Useful for ensuring callbacks from external sources are batched
Advanced Configuration
The NotifyManager also provides methods for advanced configuration (typically used by framework integrations):setNotifyFunction
Sets a custom function to wrap all notifications.- Wrapping with
React.act()in tests - Custom logging or debugging
- Integration with framework-specific batching mechanisms
setBatchNotifyFunction
Sets a custom function to batch multiple notifications together.- Integration with React’s
unstable_batchedUpdates - Custom batching strategies for different frameworks
setScheduler
Sets a custom scheduler function for async execution.- Custom timing strategies
- Integration with requestAnimationFrame
- Priority-based scheduling
Complete Example
Integration with TanStack Query
The NotifyManager is used internally throughout TanStack Query to batch state updates:Performance Benefits
- Reduced re-renders: Multiple state changes are batched into a single notification cycle
- Improved responsiveness: Notifications are scheduled asynchronously, keeping the UI responsive
- Framework integration: Works with React’s batching, Vue’s reactive system, etc.
- Predictable timing: Nested batches are flattened and executed together