Overview
TheSynchronizer<T> type defines the contract for synchronizing Stan.js state with external data sources. This enables real-time updates from sources like WebSockets, Firebase, or other reactive systems.
Type Definition
Properties
value
subscribe (optional)
update callback with the new value.
Parameters:
update: Callback function to update the store with a new valuekey: The storage key associated with this state
getSnapshot
key: The storage key to retrieve the snapshot for
update
value: The new value to sync to the external sourcekey: The storage key being updated
Usage Examples
Basic Synchronizer
WebSocket Synchronizer
Async Synchronizer with Firebase
Type Inference
The generic typeT is automatically inferred from the value property:
Best Practices
- Error Handling: Always handle errors in async
getSnapshotimplementations - Cleanup: Implement cleanup in
subscribeif your data source requires it - Type Safety: Use explicit type parameters for complex types
- Bidirectional Sync: Ensure
updateproperly persists changes to prevent data loss - Initial Value: Provide a sensible default in the
valueproperty
Related
- synchronize() - Function to create synchronized state
- Storage - Storage type for persistent state