Skip to content

Commit a7af661

Browse files
committed
keep store lock throughout whole new_or_existing_inner
1 parent 8a8bdca commit a7af661

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

plugins/store/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct ChangePayload<'a> {
4242

4343
#[derive(Debug)]
4444
pub struct StoreState {
45-
stores: Mutex<HashMap<PathBuf, ResourceId>>,
45+
stores: Arc<Mutex<HashMap<PathBuf, ResourceId>>>,
4646
serialize_fns: HashMap<String, SerializeFn>,
4747
deserialize_fns: HashMap<String, DeserializeFn>,
4848
default_serialize: SerializeFn,
@@ -477,7 +477,7 @@ impl<R: Runtime> Builder<R> {
477477
])
478478
.setup(move |app_handle, _api| {
479479
app_handle.manage(StoreState {
480-
stores: Mutex::new(HashMap::new()),
480+
stores: Arc::new(Mutex::new(HashMap::new())),
481481
serialize_fns: self.serialize_fns,
482482
deserialize_fns: self.deserialize_fns,
483483
default_serialize: self.default_serialize,

plugins/store/src/store.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
collections::HashMap,
99
fs,
1010
path::{Path, PathBuf},
11-
sync::{Arc, Mutex},
11+
sync::{Arc, Mutex, MutexGuard},
1212
time::Duration,
1313
};
1414
use tauri::{path::BaseDirectory, AppHandle, Emitter, Manager, Resource, ResourceId, Runtime};
@@ -170,10 +170,11 @@ impl<R: Runtime> StoreBuilder<R> {
170170
self
171171
}
172172

173-
pub(crate) fn build_inner(mut self, load: bool) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
174-
let state = self.app.state::<StoreState>();
175-
let mut stores = state.stores.lock().unwrap();
176-
173+
pub(crate) fn build_inner(
174+
mut self,
175+
load: bool,
176+
mut stores: MutexGuard<'_, HashMap<PathBuf, ResourceId>>,
177+
) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
177178
if stores.contains_key(&self.path) {
178179
return Err(crate::Error::AlreadyExists(self.path));
179180
}
@@ -227,7 +228,8 @@ impl<R: Runtime> StoreBuilder<R> {
227228
}
228229

229230
pub(crate) fn create_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
230-
self.build_inner(false)
231+
let stores = self.app.state::<StoreState>().stores.clone();
232+
self.build_inner(false, stores.lock().unwrap())
231233
}
232234

233235
/// Get the existing store with the same path or creates a new [`Store`].
@@ -251,14 +253,13 @@ impl<R: Runtime> StoreBuilder<R> {
251253
}
252254

253255
pub(crate) fn new_or_existing_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
254-
{
255-
let state = self.app.state::<StoreState>();
256-
let stores = state.stores.lock().unwrap();
257-
if let Some(rid) = stores.get(&self.path) {
258-
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
259-
}
256+
let stores = self.app.state::<StoreState>().stores.clone();
257+
let stores_ = stores.lock().unwrap();
258+
if let Some(rid) = stores_.get(&self.path) {
259+
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
260260
}
261-
self.build_inner(true)
261+
262+
self.build_inner(true, stores_)
262263
}
263264
}
264265

0 commit comments

Comments
 (0)