TimeoutManager
Provides a customizable interface for managing timeouts and intervals in TanStack Query. This utility allows you to replace the default globalsetTimeout and setInterval implementations with custom providers, which is useful for testing, optimization, or alternative timer implementations.
Instance
A singleton instance is exported astimeoutManager:
Why TimeoutManager?
TanStack Query makes extensive use of timeouts to implement features likestaleTime and gcTime. The default implementation uses the platform’s global setTimeout, which can have scalability issues with thousands of timeouts on the event loop.
The TimeoutManager provides a way to:
- Replace timeout implementation for better performance
- Mock timers in tests
- Implement timeout coalescing for better scalability
- Use alternative timer implementations (e.g.,
setImmediate, custom schedulers)
Methods
setTimeout
Schedules a callback to be executed after a specified delay.(_: void) => void
required
The function to execute after the delay. The callback accepts a single void argument to maintain compatibility with platform
setTimeout typings.number
required
The delay in milliseconds before executing the callback.
clearTimeout to cancel the timeout. The type is ManagedTimerId which can be a number or an object with a numeric value.
Usage:
clearTimeout
Cancels a timeout that was previously scheduled withsetTimeout.
ManagedTimerId | undefined
The timer ID returned from
setTimeout. Can be undefined for convenience (will be a no-op).setInterval
Schedules a callback to be executed repeatedly at a specified interval.(_: void) => void
required
The function to execute at each interval. The callback accepts a single void argument to maintain compatibility with platform
setInterval typings.number
required
The delay in milliseconds between each execution of the callback.
clearInterval to cancel the interval.
Usage:
clearInterval
Cancels an interval that was previously scheduled withsetInterval.
ManagedTimerId | undefined
The timer ID returned from
setInterval. Can be undefined for convenience (will be a no-op).Advanced Configuration
setTimeoutProvider
Replaces the default timeout provider with a custom implementation.TimeoutProvider<TTimerId>
required
An object implementing the TimeoutProvider interface with
setTimeout, clearTimeout, setInterval, and clearInterval methods.Type definition:clearTimeout calls may not work correctly with timer IDs from the previous provider. In development mode, a warning is logged if you switch providers after timers have been created.
Complete Examples
Example 1: Mock timers for testing
Example 2: Coalescing timeouts for better performance
Example 3: Using in TanStack Query
Default Implementation
By default, the TimeoutManager uses the platform’s global timer functions:Type Definitions
Notes
- The
TimeoutCallbacktype requires a single void argument for compatibility with Promise constructors and platform typings - Timer IDs from different providers may not be compatible - avoid switching providers after creating timers
- The TimeoutManager is used internally throughout TanStack Query for
staleTime,gcTime, retry delays, and other timing features - Custom providers must implement all four methods:
setTimeout,clearTimeout,setInterval,clearInterval
See Also
- NotifyManager - Manages notification batching and scheduling
- Testing
- Query Options