Skip to content

Commit 028d666

Browse files
committed
reintroduce "get existing store" behavior for create_or_load
1 parent afbb5aa commit 028d666

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

plugins/store/guest-js/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export async function create(
4646
}
4747

4848
/**
49-
* Create a new Store or load the existing store with the path
49+
* Create a new Store or load the existing store with the path.
50+
*
51+
* If the store at the given path is already loaded,
52+
* its instance is returned regardless of the options object.
53+
* If the settings to not match an error is returned.
5054
*
5155
* @param path Path to save the store in `app_data_dir`
5256
* @param options Store configuration options

plugins/store/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ pub trait StoreExt<R: Runtime> {
299299
/// Get a handle of an already loaded store.
300300
///
301301
/// If the store is not loaded or does not exist, it returns `None`.
302-
/// In this case, you should initialize it with [`Self::store`].
302+
///
303+
/// Note that using this function can cause race conditions if you fallback to creating or loading the store,
304+
/// so you should consider using [`Self::store`] if you are not sure if the store is loaded or not.
303305
///
304306
/// # Examples
305307
///
@@ -312,6 +314,9 @@ pub trait StoreExt<R: Runtime> {
312314
/// let store = if let Some(s) = app.get_store("store.json") {
313315
/// s
314316
/// } else {
317+
/// // this is not thread safe; if another thread is doing the same load/create,
318+
/// // there will be a race condition; in this case we could remove the get_store
319+
/// // and only run app.store() as it will return the existing store if it has been loaded
315320
/// app.store("store.json")?
316321
/// };
317322
/// Ok(())
@@ -323,9 +328,6 @@ pub trait StoreExt<R: Runtime> {
323328
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
324329
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
325330
let path = path.as_ref();
326-
if let Some(store) = self.get_store(path) {
327-
return Ok(store);
328-
}
329331
StoreBuilder::new(self.app_handle(), path).create_or_load()
330332
}
331333

plugins/store/src/store.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,11 @@ impl<R: Runtime> StoreBuilder<R> {
230230
self.build_inner(false)
231231
}
232232

233-
/// Get the existing store with the same path or creates a new [`Store`], also see [`create`](Self::create).
233+
/// Get the existing store with the same path or creates a new [`Store`].
234+
///
235+
/// If a store with the same path has already been loaded its instance is returned.
236+
///
237+
/// See [`create`](Self::create) if you want to force create a new store in the path.
234238
///
235239
/// # Examples
236240
/// ```
@@ -247,6 +251,13 @@ impl<R: Runtime> StoreBuilder<R> {
247251
}
248252

249253
pub(crate) fn create_or_load_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+
}
260+
}
250261
self.build_inner(true)
251262
}
252263
}

0 commit comments

Comments
 (0)