Function Signature
Parameters
The function takes no parameters.Returns
The function returnsvoid.
How It Works
In TanStack Store, when you update an atom or store, notifications to subscribers are normally queued and flushed automatically. Theflush() function allows you to manually trigger the processing of this notification queue.
Key behaviors:
- If currently inside a
batch(),flush()does nothing (waits for batch to complete) - Otherwise, processes all queued effect notifications immediately
- Clears the notification queue after processing
In most applications, you won’t need to call
flush() directly. The store system automatically flushes notifications at appropriate times. This is an advanced API for special use cases.Examples
Understanding Automatic Flushing
Manual Flushing in Tests
Flush Behavior with Batching
Custom Update Scheduling
Testing Synchronous Updates
Debugging Update Timing
Integration with External Systems
When to Use flush()
Testing
Ensure all updates have propagated before making assertions in tests.
Debugging
Understand exactly when notifications occur during complex update sequences.
Integration
Coordinate with external systems that need guaranteed update timing.
Custom Scheduling
Build custom update scheduling mechanisms (advanced use case).
When NOT to Use flush()
Common Misconceptions
Misconception: Need to flush after every update
Misconception: flush() forces immediate update
Implementation Details
From the source code (atom.ts:69-81):
- Respects batch depth
- Processes queued effects in order
- Clears the queue when complete
Related APIs
- batch - Batch multiple updates together
- createAtom - Create reactive atoms
- createStore - Create reactive stores