Skip to main content

Function Signature

Manually flushes all pending subscriber notifications. This is typically used in advanced scenarios where you need fine-grained control over when updates are propagated to subscribers.

Parameters

The function takes no parameters.

Returns

The function returns void.

How It Works

In TanStack Store, when you update an atom or store, notifications to subscribers are normally queued and flushed automatically. The flush() 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()

Avoid using flush() in these scenarios:
  • Normal application code - Updates flush automatically
  • Inside batch() - Has no effect; wait for batch to complete
  • Performance optimization - Calling flush() unnecessarily can hurt performance
  • As a “fix” for timing issues - Usually indicates an architectural problem

Common Misconceptions

Misconception: Need to flush after every update

Misconception: flush() forces immediate update

Implementation Details

From the source code (atom.ts:69-81):
Key points:
  • Respects batch depth
  • Processes queued effects in order
  • Clears the queue when complete