| |
| |
| |
|
|
| use crate::{ChangePayload, StoreState}; |
| use serde_json::Value as JsonValue; |
| use std::{ |
| collections::HashMap, |
| fs, |
| path::{Path, PathBuf}, |
| sync::{Arc, Mutex}, |
| time::Duration, |
| }; |
| use tauri::{path::BaseDirectory, AppHandle, Emitter, Manager, Resource, ResourceId, Runtime}; |
| use tokio::{ |
| select, |
| sync::mpsc::{unbounded_channel, UnboundedSender}, |
| time::sleep, |
| }; |
|
|
| pub type SerializeFn = |
| fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>; |
| pub type DeserializeFn = |
| fn(&[u8]) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>>; |
|
|
| pub fn resolve_store_path<R: Runtime>( |
| app: &AppHandle<R>, |
| path: impl AsRef<Path>, |
| ) -> crate::Result<PathBuf> { |
| Ok(dunce::simplified(&app.path().resolve(path, BaseDirectory::AppData)?).to_path_buf()) |
| } |
|
|
| |
| pub struct StoreBuilder<R: Runtime> { |
| app: AppHandle<R>, |
| path: PathBuf, |
| defaults: Option<HashMap<String, JsonValue>>, |
| serialize_fn: SerializeFn, |
| deserialize_fn: DeserializeFn, |
| auto_save: Option<Duration>, |
| create_new: bool, |
| override_defaults: bool, |
| } |
|
|
| impl<R: Runtime> StoreBuilder<R> { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self { |
| let app = manager.app_handle().clone(); |
| let state = app.state::<StoreState>(); |
| let serialize_fn = state.default_serialize; |
| let deserialize_fn = state.default_deserialize; |
| Self { |
| app, |
| path: path.as_ref().to_path_buf(), |
| defaults: None, |
| serialize_fn, |
| deserialize_fn, |
| auto_save: Some(Duration::from_millis(100)), |
| create_new: false, |
| override_defaults: false, |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn defaults(mut self, defaults: HashMap<String, JsonValue>) -> Self { |
| self.defaults = Some(defaults); |
| self |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn default(mut self, key: impl Into<String>, value: impl Into<JsonValue>) -> Self { |
| let key = key.into(); |
| let value = value.into(); |
| self.defaults |
| .get_or_insert(HashMap::new()) |
| .insert(key, value); |
| self |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn serialize(mut self, serialize: SerializeFn) -> Self { |
| self.serialize_fn = serialize; |
| self |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn deserialize(mut self, deserialize: DeserializeFn) -> Self { |
| self.deserialize_fn = deserialize; |
| self |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn auto_save(mut self, debounce_duration: Duration) -> Self { |
| self.auto_save = Some(debounce_duration); |
| self |
| } |
|
|
| |
| pub fn disable_auto_save(mut self) -> Self { |
| self.auto_save = None; |
| self |
| } |
|
|
| |
| pub fn create_new(mut self) -> Self { |
| self.create_new = true; |
| self |
| } |
|
|
| |
| pub fn override_defaults(mut self) -> Self { |
| self.override_defaults = true; |
| self |
| } |
|
|
| pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> { |
| let stores = self.app.state::<StoreState>().stores.clone(); |
| let mut stores = stores.lock().unwrap(); |
|
|
| self.path = resolve_store_path(&self.app, self.path)?; |
|
|
| if self.create_new { |
| if let Some(rid) = stores.remove(&self.path) { |
| let _ = self.app.resources_table().take::<Store<R>>(rid); |
| } |
| } else if let Some(rid) = stores.get(&self.path) { |
| |
| |
| |
| return Ok((self.app.resources_table().get(*rid)?, *rid)); |
| } |
|
|
| |
| |
| |
|
|
| let mut store_inner = StoreInner::new( |
| self.app.clone(), |
| self.path.clone(), |
| self.defaults.take(), |
| self.serialize_fn, |
| self.deserialize_fn, |
| ); |
|
|
| if !self.create_new { |
| if self.override_defaults { |
| let _ = store_inner.load_ignore_defaults(); |
| } else { |
| let _ = store_inner.load(); |
| } |
| } |
|
|
| let store = Store { |
| auto_save: self.auto_save, |
| auto_save_debounce_sender: Arc::new(Mutex::new(None)), |
| store: Arc::new(Mutex::new(store_inner)), |
| }; |
|
|
| let store = Arc::new(store); |
| let rid = self.app.resources_table().add_arc(store.clone()); |
| stores.insert(self.path, rid); |
|
|
| Ok((store, rid)) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn build(self) -> crate::Result<Arc<Store<R>>> { |
| let (store, _) = self.build_inner()?; |
| Ok(store) |
| } |
| } |
|
|
| enum AutoSaveMessage { |
| Reset, |
| Cancel, |
| } |
|
|
| #[derive(Clone)] |
| struct StoreInner<R: Runtime> { |
| app: AppHandle<R>, |
| path: PathBuf, |
| cache: HashMap<String, JsonValue>, |
| defaults: Option<HashMap<String, JsonValue>>, |
| serialize_fn: SerializeFn, |
| deserialize_fn: DeserializeFn, |
| } |
|
|
| impl<R: Runtime> StoreInner<R> { |
| fn new( |
| app: AppHandle<R>, |
| path: PathBuf, |
| defaults: Option<HashMap<String, JsonValue>>, |
| serialize_fn: SerializeFn, |
| deserialize_fn: DeserializeFn, |
| ) -> Self { |
| Self { |
| app, |
| path, |
| cache: defaults.clone().unwrap_or_default(), |
| defaults, |
| serialize_fn, |
| deserialize_fn, |
| } |
| } |
|
|
| |
| pub fn save(&self) -> crate::Result<()> { |
| fs::create_dir_all(self.path.parent().expect("invalid store path"))?; |
|
|
| let bytes = (self.serialize_fn)(&self.cache).map_err(crate::Error::Serialize)?; |
| fs::write(&self.path, bytes)?; |
|
|
| Ok(()) |
| } |
|
|
| |
| |
| |
| pub fn load(&mut self) -> crate::Result<()> { |
| let bytes = fs::read(&self.path)?; |
|
|
| self.cache |
| .extend((self.deserialize_fn)(&bytes).map_err(crate::Error::Deserialize)?); |
|
|
| Ok(()) |
| } |
|
|
| |
| pub fn load_ignore_defaults(&mut self) -> crate::Result<()> { |
| let bytes = fs::read(&self.path)?; |
| self.cache = (self.deserialize_fn)(&bytes).map_err(crate::Error::Deserialize)?; |
| Ok(()) |
| } |
|
|
| |
| pub fn set(&mut self, key: impl Into<String>, value: impl Into<JsonValue>) { |
| let key = key.into(); |
| let value = value.into(); |
| self.cache.insert(key.clone(), value.clone()); |
| let _ = self.emit_change_event(&key, Some(&value)); |
| } |
|
|
| |
| pub fn get(&self, key: impl AsRef<str>) -> Option<&JsonValue> { |
| self.cache.get(key.as_ref()) |
| } |
|
|
| |
| pub fn has(&self, key: impl AsRef<str>) -> bool { |
| self.cache.contains_key(key.as_ref()) |
| } |
|
|
| |
| pub fn delete(&mut self, key: impl AsRef<str>) -> bool { |
| let flag = self.cache.remove(key.as_ref()).is_some(); |
| if flag { |
| let _ = self.emit_change_event(key.as_ref(), None); |
| } |
| flag |
| } |
|
|
| |
| |
| |
| pub fn clear(&mut self) { |
| let keys: Vec<String> = self.cache.keys().cloned().collect(); |
| self.cache.clear(); |
| for key in &keys { |
| let _ = self.emit_change_event(key, None); |
| } |
| } |
|
|
| |
| |
| |
| pub fn reset(&mut self) { |
| if let Some(defaults) = &self.defaults { |
| for (key, value) in &self.cache { |
| if defaults.get(key) != Some(value) { |
| let _ = self.emit_change_event(key, defaults.get(key)); |
| } |
| } |
| for (key, value) in defaults { |
| if !self.cache.contains_key(key) { |
| let _ = self.emit_change_event(key, Some(value)); |
| } |
| } |
| self.cache.clone_from(defaults); |
| } else { |
| self.clear() |
| } |
| } |
|
|
| |
| pub fn keys(&self) -> impl Iterator<Item = &String> { |
| self.cache.keys() |
| } |
|
|
| |
| pub fn values(&self) -> impl Iterator<Item = &JsonValue> { |
| self.cache.values() |
| } |
|
|
| |
| pub fn entries(&self) -> impl Iterator<Item = (&String, &JsonValue)> { |
| self.cache.iter() |
| } |
|
|
| |
| pub fn len(&self) -> usize { |
| self.cache.len() |
| } |
|
|
| |
| pub fn is_empty(&self) -> bool { |
| self.cache.is_empty() |
| } |
|
|
| fn emit_change_event(&self, key: &str, value: Option<&JsonValue>) -> crate::Result<()> { |
| let state = self.app.state::<StoreState>(); |
| let stores = state.stores.lock().unwrap(); |
| let exists = value.is_some(); |
| self.app.emit( |
| "store://change", |
| ChangePayload { |
| path: &self.path, |
| resource_id: stores.get(&self.path).copied(), |
| key, |
| value, |
| exists, |
| }, |
| )?; |
| Ok(()) |
| } |
| } |
|
|
| impl<R: Runtime> std::fmt::Debug for StoreInner<R> { |
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| f.debug_struct("Store") |
| .field("path", &self.path) |
| .field("cache", &self.cache) |
| .finish() |
| } |
| } |
|
|
| pub struct Store<R: Runtime> { |
| auto_save: Option<Duration>, |
| auto_save_debounce_sender: Arc<Mutex<Option<UnboundedSender<AutoSaveMessage>>>>, |
| store: Arc<Mutex<StoreInner<R>>>, |
| } |
|
|
| impl<R: Runtime> Resource for Store<R> { |
| fn close(self: Arc<Self>) { |
| let store = self.store.lock().unwrap(); |
| let state = store.app.state::<StoreState>(); |
| let mut stores = state.stores.lock().unwrap(); |
| stores.remove(&store.path); |
| } |
| } |
|
|
| impl<R: Runtime> Store<R> { |
| |
| |
| |
| |
| |
| |
|
|
| |
| pub fn set(&self, key: impl Into<String>, value: impl Into<JsonValue>) { |
| self.store.lock().unwrap().set(key.into(), value.into()); |
| let _ = self.trigger_auto_save(); |
| } |
|
|
| |
| pub fn get(&self, key: impl AsRef<str>) -> Option<JsonValue> { |
| self.store.lock().unwrap().get(key).cloned() |
| } |
|
|
| |
| pub fn has(&self, key: impl AsRef<str>) -> bool { |
| self.store.lock().unwrap().has(key) |
| } |
|
|
| |
| pub fn delete(&self, key: impl AsRef<str>) -> bool { |
| let deleted = self.store.lock().unwrap().delete(key); |
| if deleted { |
| let _ = self.trigger_auto_save(); |
| } |
| deleted |
| } |
|
|
| |
| |
| |
| pub fn clear(&self) { |
| self.store.lock().unwrap().clear(); |
| let _ = self.trigger_auto_save(); |
| } |
|
|
| |
| |
| |
| pub fn reset(&self) { |
| self.store.lock().unwrap().reset(); |
| let _ = self.trigger_auto_save(); |
| } |
|
|
| |
| pub fn keys(&self) -> Vec<String> { |
| self.store.lock().unwrap().keys().cloned().collect() |
| } |
|
|
| |
| pub fn values(&self) -> Vec<JsonValue> { |
| self.store.lock().unwrap().values().cloned().collect() |
| } |
|
|
| |
| pub fn entries(&self) -> Vec<(String, JsonValue)> { |
| self.store |
| .lock() |
| .unwrap() |
| .entries() |
| .map(|(k, v)| (k.to_owned(), v.to_owned())) |
| .collect() |
| } |
|
|
| |
| pub fn length(&self) -> usize { |
| self.store.lock().unwrap().len() |
| } |
|
|
| |
| pub fn is_empty(&self) -> bool { |
| self.store.lock().unwrap().is_empty() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| pub fn reload(&self) -> crate::Result<()> { |
| self.store.lock().unwrap().load() |
| } |
|
|
| |
| |
| |
| pub fn reload_ignore_defaults(&self) -> crate::Result<()> { |
| self.store.lock().unwrap().load_ignore_defaults() |
| } |
|
|
| |
| pub fn save(&self) -> crate::Result<()> { |
| if let Some(sender) = self.auto_save_debounce_sender.lock().unwrap().take() { |
| let _ = sender.send(AutoSaveMessage::Cancel); |
| } |
| self.store.lock().unwrap().save() |
| } |
|
|
| |
| pub fn close_resource(&self) { |
| let store = self.store.lock().unwrap(); |
| let app = store.app.clone(); |
| let state = app.state::<StoreState>(); |
| let stores = state.stores.lock().unwrap(); |
| if let Some(rid) = stores.get(&store.path).copied() { |
| drop(store); |
| drop(stores); |
| let _ = app.resources_table().close(rid); |
| } |
| } |
|
|
| fn trigger_auto_save(&self) -> crate::Result<()> { |
| let Some(auto_save_delay) = self.auto_save else { |
| return Ok(()); |
| }; |
| if auto_save_delay.is_zero() { |
| return self.save(); |
| } |
| let mut auto_save_debounce_sender = self.auto_save_debounce_sender.lock().unwrap(); |
| if let Some(ref sender) = *auto_save_debounce_sender { |
| let _ = sender.send(AutoSaveMessage::Reset); |
| return Ok(()); |
| } |
| let (sender, mut receiver) = unbounded_channel(); |
| auto_save_debounce_sender.replace(sender); |
| drop(auto_save_debounce_sender); |
| let store = self.store.clone(); |
| let auto_save_debounce_sender = self.auto_save_debounce_sender.clone(); |
| tauri::async_runtime::spawn(async move { |
| loop { |
| select! { |
| should_cancel = receiver.recv() => { |
| if matches!(should_cancel, Some(AutoSaveMessage::Cancel) | None) { |
| return; |
| } |
| } |
| _ = sleep(auto_save_delay) => { |
| auto_save_debounce_sender.lock().unwrap().take(); |
| let _ = store.lock().unwrap().save(); |
| return; |
| } |
| }; |
| } |
| }); |
| Ok(()) |
| } |
|
|
| fn apply_pending_auto_save(&self) { |
| |
| if let Some(sender) = self.auto_save_debounce_sender.lock().unwrap().take() { |
| let _ = sender.send(AutoSaveMessage::Cancel); |
| let _ = self.save(); |
| }; |
| } |
| } |
|
|
| impl<R: Runtime> Drop for Store<R> { |
| fn drop(&mut self) { |
| self.apply_pending_auto_save(); |
| } |
| } |
|
|