synced() function creates an observable that automatically syncs with local persistence and remote data sources. It’s a simplified wrapper around syncObservable() for common use cases.
Usage
Parameters
Configuration for syncing behavior
SyncedOptions
Function to fetch data from remote source
Function to save changes to remote source
Function to subscribe to real-time updates
Local persistence configuration. See persist plugins for details.
Initial value before data loads
Transform data between local and remote formats
How to apply remote data to the observable
set: Replace entire valueassign: Shallow merge (Object.assign)merge: Deep mergeappend: Add to end of arrayprepend: Add to beginning of array
Debounce remote saves by this many milliseconds
auto: Sync automatically when observable is accessedmanual: Only sync whensync()is called
Retry configuration for failed operations
Wait for condition before syncing get/subscribe
Wait for condition before saving changes
Called before fetching remote data
Called before saving changes
Called after changes are saved
Error handler for sync operations
Return Value
Returns aSynced<TLocal> observable with the following characteristics:
- Automatically loads from local persistence on first access
- Syncs with remote data source based on
syncMode - Tracks sync state via
syncState(observable) - Persists changes locally and remotely
SyncedGetParams
Parameters passed to theget function:
Internal node information
The observable being synced
Function to trigger a refresh
Current value of the observable
Timestamp of last successful sync
Update the last sync timestamp
How to apply the loaded data
Error handler
Current retry attempt number
Set to true to cancel retrying
SyncedSetParams
Parameters passed to theset function:
Internal node information
The observable being synced
Function to trigger a refresh
The value to save
Array of changes that triggered this saveEach change contains:
path: Array of keys to the changed valuepathTypes: Types of each path segmentvalueAtPath: New value at this pathprevAtPath: Previous value at this pathpathStr: String representation of path
Update the observable with changes from the server
Error handler
Current retry attempt number
Set to true to cancel retrying
SyncedSubscribeParams
Parameters passed to thesubscribe function:
Internal node information
The observable being synced
Function to trigger a refresh
Timestamp of last successful sync
Update the observable with new data
Error handler
Examples
Simple GET/SET
With Transform
With Real-time Updates
Function Shorthand
See Also
- syncObservable() - Lower-level API for syncing existing observables
- configureObservableSync() - Global sync configuration
- Sync & Persistence Guide
- Persist Plugins