pub struct SyncedStore<T> { /* private fields */ }Expand description
A synchronized key-value store with device-owned slices.
Each device owns one slice of type T. Writes update only the local
slice and broadcast the change. Remote slices are received and cached
automatically.
Created via SyncedStore::new or Node::synced_store.
Implementations§
Source§impl<T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static> SyncedStore<T>
impl<T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static> SyncedStore<T>
Sourcepub fn new<N: NetworkProvider + 'static>(
node: Arc<Node<N>>,
store_id: &str,
) -> Arc<Self>
pub fn new<N: NetworkProvider + 'static>( node: Arc<Node<N>>, store_id: &str, ) -> Arc<Self>
Create a new SyncedStore and start the background sync task.
Uses the default MemoryBackend (no persistence). For durable
storage across restarts, use new_with_backend.
The store syncs on namespace "ss:{store_id}". The caller must hold
an Arc<Node<N>> because the background task needs to outlive this
constructor call.
Sourcepub fn new_with_backend<N: NetworkProvider + 'static>(
node: Arc<Node<N>>,
store_id: &str,
backend: Arc<dyn StoreBackend>,
) -> Arc<Self>
pub fn new_with_backend<N: NetworkProvider + 'static>( node: Arc<Node<N>>, store_id: &str, backend: Arc<dyn StoreBackend>, ) -> Arc<Self>
Create a new SyncedStore with a custom persistence backend.
On startup, attempts to load the local device’s persisted slice. If found, the in-memory state and version counter are restored so the store picks up where it left off.
Sourcepub async fn set(&self, data: T)
pub async fn set(&self, data: T)
Update this device’s data. Increments version and broadcasts.
Rapid successive calls coalesce on the wire: peers are guaranteed to observe the newest version, not every intermediate one (the store is last-write-wins state, not an event log).
Sourcepub async fn local(&self) -> Option<T>
pub async fn local(&self) -> Option<T>
This device’s current data, or None if set() hasn’t been called.
Sourcepub async fn get(&self, device_id: &str) -> Option<Slice<T>>
pub async fn get(&self, device_id: &str) -> Option<Slice<T>>
A specific peer’s slice, or None if we haven’t received their data.
Sourcepub async fn device_ids(&self) -> Vec<String>
pub async fn device_ids(&self) -> Vec<String>
Device IDs that have data in this store (local + remote).
Sourcepub fn subscribe(&self) -> Receiver<StoreEvent<T>>
pub fn subscribe(&self) -> Receiver<StoreEvent<T>>
Subscribe to store change events.