Skip to main content

truffle_core/synced_store/
backend.rs

1//! Persistence backends for SyncedStore.
2
3/// Backend for persisting store data across restarts.
4///
5/// Methods are synchronous — file I/O for small JSON payloads is fast enough
6/// that async overhead isn't warranted. Apps needing async persistence (e.g.,
7/// database-backed) can spawn a blocking task internally.
8pub trait StoreBackend: Send + Sync + 'static {
9    /// Load a device's slice. Returns `(serialized_data, version)` or `None`.
10    fn load(&self, store_id: &str, device_id: &str) -> Option<(Vec<u8>, u64)>;
11
12    /// Save a device's slice.
13    fn save(&self, store_id: &str, device_id: &str, data: &[u8], version: u64);
14
15    /// Remove a device's slice.
16    fn remove(&self, store_id: &str, device_id: &str);
17}
18
19/// In-memory backend (no persistence). Default.
20///
21/// Data lives only in the `SyncedStore`'s in-memory state and is lost on
22/// process exit. Suitable for ephemeral stores like presence or typing
23/// indicators.
24#[derive(Debug, Default)]
25pub struct MemoryBackend;
26
27impl StoreBackend for MemoryBackend {
28    fn load(&self, _store_id: &str, _device_id: &str) -> Option<(Vec<u8>, u64)> {
29        None
30    }
31
32    fn save(&self, _store_id: &str, _device_id: &str, _data: &[u8], _version: u64) {}
33
34    fn remove(&self, _store_id: &str, _device_id: &str) {}
35}