From 98d4fc4e9b61fabe5e3150da9fe164bff34be881 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 3 Oct 2024 09:33:42 +0800 Subject: [PATCH] Remove share store --- plugins/store/src/lib.rs | 66 ++++++++++------------------------------ 1 file changed, 16 insertions(+), 50 deletions(-) diff --git a/plugins/store/src/lib.rs b/plugins/store/src/lib.rs index 63951190d..713d767df 100644 --- a/plugins/store/src/lib.rs +++ b/plugins/store/src/lib.rs @@ -18,7 +18,7 @@ pub use serde_json::Value as JsonValue; use std::{ collections::HashMap, path::{Path, PathBuf}, - sync::{Arc, Mutex, Weak}, + sync::{Arc, Mutex}, time::Duration, }; pub use store::{Store, StoreBuilder, StoreInner}; @@ -37,9 +37,8 @@ struct ChangePayload<'a> { value: &'a JsonValue, } -pub struct StoreCollection { - /// This weak pointer is always pointing to a real reference since we will remove it on drop - stores: Mutex>, Option)>>, +pub struct StoreCollection { + stores: Mutex>, } #[derive(Serialize, Deserialize)] @@ -67,32 +66,15 @@ async fn create_store( _ => {} } } - let store = builder.build()?; - let rid = app.resources_table().add_arc(store); - let collection = app.state::>(); - let mut stores = collection.stores.lock().unwrap(); - if let Some((_, resource_id)) = stores.get_mut(&path) { - resource_id.replace(rid); - } + let (_, rid) = builder.build_inner()?; Ok(rid) } #[tauri::command] async fn get_store(app: AppHandle, path: PathBuf) -> Option { - let collection = app.state::>(); - let mut stores = collection.stores.lock().unwrap(); - if let Some((store, resource_id)) = stores.get_mut(&path) { - let rid = if let Some(resource_id) = resource_id { - *resource_id - } else { - let rid = app.resources_table().add_arc(store.upgrade().unwrap()); - resource_id.replace(rid); - rid - }; - Some(rid) - } else { - None - } + let collection = app.state::(); + let stores = collection.stores.lock().unwrap(); + stores.get(&path).copied() } #[tauri::command] @@ -185,7 +167,6 @@ async fn save(app: AppHandle, rid: ResourceId) -> Result<()> { pub trait StoreExt { fn create_store(&self, path: impl AsRef) -> Result>>; fn store_builder(&self, path: impl AsRef) -> StoreBuilder; - fn share_store(&self, store: Arc>); fn get_store(&self, path: impl AsRef) -> Option>>; } @@ -198,28 +179,12 @@ impl> StoreExt for T { StoreBuilder::new(self.app_handle(), path) } - fn share_store(&self, store: Arc>) { - let collection = self.state::>(); - let mut stores = collection.stores.lock().unwrap(); - if let Some(path) = store.with_store(|inner_store| { - if stores.contains_key(&inner_store.path) { - None - } else { - Some(inner_store.path.clone()) - } - }) { - let weak_store = Arc::downgrade(&store); - let rid = self.resources_table().add_arc(store); - stores.insert(path, (weak_store, Some(rid))); - } - } - fn get_store(&self, path: impl AsRef) -> Option>> { - let collection = self.state::>(); + let collection = self.state::(); let stores = collection.stores.lock().unwrap(); stores .get(path.as_ref()) - .and_then(|(store, _)| store.upgrade()) + .and_then(|rid| self.resources_table().get(*rid).ok()) } } @@ -281,7 +246,7 @@ impl Builder { } } - app_handle.manage(StoreCollection:: { + app_handle.manage(StoreCollection { stores: Mutex::new(HashMap::new()), }); @@ -289,12 +254,13 @@ impl Builder { }) .on_event(|app_handle, event| { if let RunEvent::Exit = event { - let collection = app_handle.state::>(); + let collection = app_handle.state::(); let stores = collection.stores.lock().unwrap(); - for (path, (store, _)) in stores.iter() { - let store = store.upgrade().unwrap(); - if let Err(err) = store.save() { - eprintln!("failed to save store {path:?} with error {err:?}"); + for (path, rid) in stores.iter() { + if let Ok(store) = app_handle.resources_table().get::>(*rid) { + if let Err(err) = store.save() { + eprintln!("failed to save store {path:?} with error {err:?}"); + } } } }