mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-12 16:36:33 +02:00
feat(store): load override defaults (#2857)
* feat(store): load override defaults * Update docs * Update example * Allow setting defaults from js * Tweak resolve * Merge remote-tracking branch 'upstream/v2' into store-load-override-defaults * Merge branch 'v2' of https://github.com/tauri-apps/plugins-workspace into store-load-override-defaults * Merge branch 'v2' into store-load-override-defaults * Rename to ignore defaults * Merge remote-tracking branch 'upstream/v2' into store-load-override-defaults
This commit is contained in:
@@ -39,6 +39,7 @@ pub struct StoreBuilder<R: Runtime> {
|
||||
deserialize_fn: DeserializeFn,
|
||||
auto_save: Option<Duration>,
|
||||
create_new: bool,
|
||||
override_defaults: bool,
|
||||
}
|
||||
|
||||
impl<R: Runtime> StoreBuilder<R> {
|
||||
@@ -66,6 +67,7 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
deserialize_fn,
|
||||
auto_save: Some(Duration::from_millis(100)),
|
||||
create_new: false,
|
||||
override_defaults: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,6 +180,12 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Override the store values when creating the store, ignoring defaults.
|
||||
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();
|
||||
@@ -205,7 +213,11 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
);
|
||||
|
||||
if !self.create_new {
|
||||
let _ = store_inner.load();
|
||||
if self.override_defaults {
|
||||
let _ = store_inner.load_ignore_defaults();
|
||||
} else {
|
||||
let _ = store_inner.load();
|
||||
}
|
||||
}
|
||||
|
||||
let store = Store {
|
||||
@@ -284,6 +296,8 @@ impl<R: Runtime> StoreInner<R> {
|
||||
}
|
||||
|
||||
/// Update the store from the on-disk state
|
||||
///
|
||||
/// Note: This method loads the data and merges it with the current store
|
||||
pub fn load(&mut self) -> crate::Result<()> {
|
||||
let bytes = fs::read(&self.path)?;
|
||||
|
||||
@@ -293,6 +307,13 @@ impl<R: Runtime> StoreInner<R> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Load the store from the on-disk state, ignoring defaults
|
||||
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(())
|
||||
}
|
||||
|
||||
/// Inserts a key-value pair into the store.
|
||||
pub fn set(&mut self, key: impl Into<String>, value: impl Into<JsonValue>) {
|
||||
let key = key.into();
|
||||
@@ -499,10 +520,24 @@ impl<R: Runtime> Store<R> {
|
||||
}
|
||||
|
||||
/// Update the store from the on-disk state
|
||||
///
|
||||
/// Note:
|
||||
/// - This method loads the data and merges it with the current store,
|
||||
/// this behavior will be changed to resetting to default first and then merging with the on-disk state in v3,
|
||||
/// to fully match the store with the on-disk state,
|
||||
/// use [`reload_override_defaults`](Self::reload_override_defaults) instead
|
||||
/// - This method does not emit change events
|
||||
pub fn reload(&self) -> crate::Result<()> {
|
||||
self.store.lock().unwrap().load()
|
||||
}
|
||||
|
||||
/// Load the store from the on-disk state, ignoring defaults
|
||||
///
|
||||
/// Note: This method does not emit change events
|
||||
pub fn reload_ignore_defaults(&self) -> crate::Result<()> {
|
||||
self.store.lock().unwrap().load_ignore_defaults()
|
||||
}
|
||||
|
||||
/// Saves the store to disk at the store's `path`.
|
||||
pub fn save(&self) -> crate::Result<()> {
|
||||
if let Some(sender) = self.auto_save_debounce_sender.lock().unwrap().take() {
|
||||
|
||||
Reference in New Issue
Block a user