File tree Expand file tree Collapse file tree 3 files changed +23
-6
lines changed Expand file tree Collapse file tree 3 files changed +23
-6
lines changed Original file line number Diff line number Diff line change @@ -46,7 +46,11 @@ export async function create(
46
46
}
47
47
48
48
/**
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.
50
54
*
51
55
* @param path Path to save the store in `app_data_dir`
52
56
* @param options Store configuration options
Original file line number Diff line number Diff line change @@ -299,7 +299,9 @@ pub trait StoreExt<R: Runtime> {
299
299
/// Get a handle of an already loaded store.
300
300
///
301
301
/// 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.
303
305
///
304
306
/// # Examples
305
307
///
@@ -312,6 +314,9 @@ pub trait StoreExt<R: Runtime> {
312
314
/// let store = if let Some(s) = app.get_store("store.json") {
313
315
/// s
314
316
/// } 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
315
320
/// app.store("store.json")?
316
321
/// };
317
322
/// Ok(())
@@ -323,9 +328,6 @@ pub trait StoreExt<R: Runtime> {
323
328
impl < R : Runtime , T : Manager < R > > StoreExt < R > for T {
324
329
fn store ( & self , path : impl AsRef < Path > ) -> Result < Arc < Store < R > > > {
325
330
let path = path. as_ref ( ) ;
326
- if let Some ( store) = self . get_store ( path) {
327
- return Ok ( store) ;
328
- }
329
331
StoreBuilder :: new ( self . app_handle ( ) , path) . create_or_load ( )
330
332
}
331
333
Original file line number Diff line number Diff line change @@ -230,7 +230,11 @@ impl<R: Runtime> StoreBuilder<R> {
230
230
self . build_inner ( false )
231
231
}
232
232
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.
234
238
///
235
239
/// # Examples
236
240
/// ```
@@ -247,6 +251,13 @@ impl<R: Runtime> StoreBuilder<R> {
247
251
}
248
252
249
253
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
+ }
250
261
self . build_inner ( true )
251
262
}
252
263
}
You can’t perform that action at this time.
0 commit comments