keep store lock throughout whole new_or_existing_inner

pull/1860/head
Lucas Nogueira 8 months ago
parent 8a8bdcac23
commit a7af6612f2
No known key found for this signature in database
GPG Key ID: 7C32FCA95C8C95D7

@ -42,7 +42,7 @@ struct ChangePayload<'a> {
#[derive(Debug)]
pub struct StoreState {
stores: Mutex<HashMap<PathBuf, ResourceId>>,
stores: Arc<Mutex<HashMap<PathBuf, ResourceId>>>,
serialize_fns: HashMap<String, SerializeFn>,
deserialize_fns: HashMap<String, DeserializeFn>,
default_serialize: SerializeFn,
@ -477,7 +477,7 @@ impl<R: Runtime> Builder<R> {
])
.setup(move |app_handle, _api| {
app_handle.manage(StoreState {
stores: Mutex::new(HashMap::new()),
stores: Arc::new(Mutex::new(HashMap::new())),
serialize_fns: self.serialize_fns,
deserialize_fns: self.deserialize_fns,
default_serialize: self.default_serialize,

@ -8,7 +8,7 @@ use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
sync::{Arc, Mutex},
sync::{Arc, Mutex, MutexGuard},
time::Duration,
};
use tauri::{path::BaseDirectory, AppHandle, Emitter, Manager, Resource, ResourceId, Runtime};
@ -170,10 +170,11 @@ impl<R: Runtime> StoreBuilder<R> {
self
}
pub(crate) fn build_inner(mut self, load: bool) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
let state = self.app.state::<StoreState>();
let mut stores = state.stores.lock().unwrap();
pub(crate) fn build_inner(
mut self,
load: bool,
mut stores: MutexGuard<'_, HashMap<PathBuf, ResourceId>>,
) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
if stores.contains_key(&self.path) {
return Err(crate::Error::AlreadyExists(self.path));
}
@ -227,7 +228,8 @@ impl<R: Runtime> StoreBuilder<R> {
}
pub(crate) fn create_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
self.build_inner(false)
let stores = self.app.state::<StoreState>().stores.clone();
self.build_inner(false, stores.lock().unwrap())
}
/// Get the existing store with the same path or creates a new [`Store`].
@ -251,14 +253,13 @@ impl<R: Runtime> StoreBuilder<R> {
}
pub(crate) fn new_or_existing_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
{
let state = self.app.state::<StoreState>();
let stores = state.stores.lock().unwrap();
if let Some(rid) = stores.get(&self.path) {
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
}
let stores = self.app.state::<StoreState>().stores.clone();
let stores_ = stores.lock().unwrap();
if let Some(rid) = stores_.get(&self.path) {
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
}
self.build_inner(true)
self.build_inner(true, stores_)
}
}

Loading…
Cancel
Save