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.
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.
import { createAtom, flush } from '@tanstack/store'const dataAtom = createAtom<any[]>([])// Accumulate updates and flush on animation framelet hasPendingFlush = falsefunction scheduleFlush() { if (!hasPendingFlush) { hasPendingFlush = true requestAnimationFrame(() => { flush() hasPendingFlush = false }) }}// Note: This is a simplified example// Real implementation would need to intercept the update mechanismdataAtom.subscribe(() => { console.log('Data updated')})
import { createAtom, flush } from '@tanstack/store'const stateAtom = createAtom({ count: 0 })// Sync with external state managementfunction syncWithExternal(externalState: any) { // Update our atom stateAtom.set(externalState) // Ensure sync is complete before continuing flush() // Now safe to read derived state console.log('Synced:', stateAtom.get())}
// ❌ flush() doesn't make updates synchronous during batchimport { batch, createStore, flush } from '@tanstack/store'const store = createStore(0)batch(() => { store.setState(() => 1) flush() // Does nothing here console.log(store.state) // Still 1, but subscribers not notified yet})// Subscribers notified here, after batch