@@ -112,7 +112,7 @@ async fn create_store<R: Runtime>(
112
112
serialize_fn_name,
113
113
deserialize_fn_name,
114
114
) ?;
115
- let ( _, rid) = builder. build_inner ( ) ?;
115
+ let ( _, rid) = builder. create_inner ( ) ?;
116
116
Ok ( rid)
117
117
}
118
118
@@ -133,7 +133,7 @@ async fn create_or_load<R: Runtime>(
133
133
serialize_fn_name,
134
134
deserialize_fn_name,
135
135
) ?;
136
- let ( _, rid) = builder. build_or_existing_inner ( ) ?;
136
+ let ( _, rid) = builder. create_or_load_inner ( ) ?;
137
137
Ok ( rid)
138
138
}
139
139
@@ -242,23 +242,95 @@ async fn save<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
242
242
}
243
243
244
244
pub trait StoreExt < R : Runtime > {
245
- /// Create a store or get an existing store with default settings at path
245
+ /// Create a store or load an existing store with default settings at the given path.
246
+ ///
247
+ /// If the store is already loaded, its instance is automatically returned.
248
+ ///
249
+ /// # Examples
250
+ ///
251
+ /// ```
252
+ /// use tauri_plugin_store::StoreExt;
253
+ ///
254
+ /// tauri::Builder::default()
255
+ /// .plugin(tauri_plugin_store::Builder::default().build())
256
+ /// .setup(|app| {
257
+ /// let store = app.store("my-store")?;
258
+ /// Ok(())
259
+ /// });
260
+ /// ```
246
261
fn store ( & self , path : impl AsRef < Path > ) -> Result < Arc < Store < R > > > ;
247
- /// Create a store with default settings
262
+ /// Create a store with default settings.
263
+ ///
264
+ /// If the store is already loaded you must check with [`Self::get_store`] or prefer [`Self::store`]
265
+ /// as it will return `Err(Error::AlreadyExists)`.
266
+ ///
267
+ /// # Examples
268
+ ///
269
+ /// ```
270
+ /// use tauri_plugin_store::StoreExt;
271
+ ///
272
+ /// tauri::Builder::default()
273
+ /// .plugin(tauri_plugin_store::Builder::default().build())
274
+ /// .setup(|app| {
275
+ /// let store = app.create_store("my-store")?;
276
+ /// Ok(())
277
+ /// });
278
+ /// ```
248
279
fn create_store ( & self , path : impl AsRef < Path > ) -> Result < Arc < Store < R > > > ;
249
- /// Get a store builder
280
+ /// Get a store builder.
281
+ ///
282
+ /// The builder can be used to configure the store.
283
+ /// To use the default settings see [`Self::store`].
284
+ ///
285
+ /// # Examples
286
+ ///
287
+ /// ```
288
+ /// use tauri_plugin_store::StoreExt;
289
+ /// use std::time::Duration;
290
+ ///
291
+ /// tauri::Builder::default()
292
+ /// .plugin(tauri_plugin_store::Builder::default().build())
293
+ /// .setup(|app| {
294
+ /// let store = app.store_builder("users.json").auto_save(Duration::from_secs(1)).create_or_load()?;
295
+ /// Ok(())
296
+ /// });
297
+ /// ```
250
298
fn store_builder ( & self , path : impl AsRef < Path > ) -> StoreBuilder < R > ;
251
- /// Get an existing store
299
+ /// Get a handle of an already loaded store.
300
+ ///
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`].
303
+ ///
304
+ /// # Examples
305
+ ///
306
+ /// ```
307
+ /// use tauri_plugin_store::StoreExt;
308
+ ///
309
+ /// tauri::Builder::default()
310
+ /// .plugin(tauri_plugin_store::Builder::default().build())
311
+ /// .setup(|app| {
312
+ /// let store = if let Some(s) = app.get_store("store.json") {
313
+ /// s
314
+ /// } else {
315
+ /// app.store("store.json")?
316
+ /// };
317
+ /// Ok(())
318
+ /// });
319
+ /// ```
252
320
fn get_store ( & self , path : impl AsRef < Path > ) -> Option < Arc < Store < R > > > ;
253
321
}
254
322
255
323
impl < R : Runtime , T : Manager < R > > StoreExt < R > for T {
256
324
fn store ( & self , path : impl AsRef < Path > ) -> Result < Arc < Store < R > > > {
257
- StoreBuilder :: new ( self . app_handle ( ) , path) . build_or_existing ( )
325
+ let path = path. as_ref ( ) ;
326
+ if let Some ( store) = self . get_store ( path) {
327
+ return Ok ( store) ;
328
+ }
329
+ StoreBuilder :: new ( self . app_handle ( ) , path) . create_or_load ( )
258
330
}
259
331
260
332
fn create_store ( & self , path : impl AsRef < Path > ) -> Result < Arc < Store < R > > > {
261
- StoreBuilder :: new ( self . app_handle ( ) , path) . build ( )
333
+ StoreBuilder :: new ( self . app_handle ( ) , path) . create ( )
262
334
}
263
335
264
336
fn store_builder ( & self , path : impl AsRef < Path > ) -> StoreBuilder < R > {
@@ -327,7 +399,7 @@ impl<R: Runtime> Builder<R> {
327
399
/// tauri_plugin_store::Builder::default()
328
400
/// .register_serialize_fn("no-pretty-json".to_owned(), no_pretty_json)
329
401
/// .build(),
330
- /// )
402
+ /// );
331
403
/// ```
332
404
pub fn register_serialize_fn ( mut self , name : String , serialize_fn : SerializeFn ) -> Self {
333
405
self . serialize_fns . insert ( name, serialize_fn) ;
@@ -356,7 +428,7 @@ impl<R: Runtime> Builder<R> {
356
428
/// tauri_plugin_store::Builder::default()
357
429
/// .default_serialize_fn(no_pretty_json)
358
430
/// .build(),
359
- /// )
431
+ /// );
360
432
/// ```
361
433
pub fn default_serialize_fn ( mut self , serialize_fn : SerializeFn ) -> Self {
362
434
self . default_serialize = serialize_fn;
@@ -377,7 +449,7 @@ impl<R: Runtime> Builder<R> {
377
449
/// tauri::Builder::default()
378
450
/// .plugin(tauri_plugin_store::Builder::default().build())
379
451
/// .setup(|app| {
380
- /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build ()?;
452
+ /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").create_or_load ()?;
381
453
/// Ok(())
382
454
/// });
383
455
/// ```
0 commit comments