@@ -8,7 +8,7 @@ use std::{
8
8
collections:: HashMap ,
9
9
fs,
10
10
path:: { Path , PathBuf } ,
11
- sync:: { Arc , Mutex , MutexGuard } ,
11
+ sync:: { Arc , Mutex } ,
12
12
time:: Duration ,
13
13
} ;
14
14
use tauri:: { path:: BaseDirectory , AppHandle , Emitter , Manager , Resource , ResourceId , Runtime } ;
@@ -38,6 +38,7 @@ pub struct StoreBuilder<R: Runtime> {
38
38
serialize_fn : SerializeFn ,
39
39
deserialize_fn : DeserializeFn ,
40
40
auto_save : Option < Duration > ,
41
+ create_new : bool ,
41
42
}
42
43
43
44
impl < R : Runtime > StoreBuilder < R > {
@@ -64,6 +65,7 @@ impl<R: Runtime> StoreBuilder<R> {
64
65
serialize_fn,
65
66
deserialize_fn,
66
67
auto_save : Some ( Duration :: from_millis ( 100 ) ) ,
68
+ create_new : false ,
67
69
}
68
70
}
69
71
@@ -79,7 +81,7 @@ impl<R: Runtime> StoreBuilder<R> {
79
81
///
80
82
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
81
83
/// .defaults(defaults)
82
- /// .load ()?;
84
+ /// .build ()?;
83
85
/// Ok(())
84
86
/// });
85
87
/// ```
@@ -97,7 +99,7 @@ impl<R: Runtime> StoreBuilder<R> {
97
99
/// .setup(|app| {
98
100
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
99
101
/// .default("foo".to_string(), "bar")
100
- /// .load ()?;
102
+ /// .build ()?;
101
103
/// Ok(())
102
104
/// });
103
105
/// ```
@@ -119,7 +121,7 @@ impl<R: Runtime> StoreBuilder<R> {
119
121
/// .setup(|app| {
120
122
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
121
123
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
122
- /// .load ()?;
124
+ /// .build ()?;
123
125
/// Ok(())
124
126
/// });
125
127
/// ```
@@ -137,7 +139,7 @@ impl<R: Runtime> StoreBuilder<R> {
137
139
/// .setup(|app| {
138
140
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
139
141
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
140
- /// .load ()?;
142
+ /// .build ()?;
141
143
/// Ok(())
142
144
/// });
143
145
/// ```
@@ -155,7 +157,7 @@ impl<R: Runtime> StoreBuilder<R> {
155
157
/// .setup(|app| {
156
158
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
157
159
/// .auto_save(std::time::Duration::from_millis(100))
158
- /// .load ()?;
160
+ /// .build ()?;
159
161
/// Ok(())
160
162
/// });
161
163
/// ```
@@ -164,16 +166,25 @@ impl<R: Runtime> StoreBuilder<R> {
164
166
self
165
167
}
166
168
167
- /// Disable auto save on modified with a debounce duration
169
+ /// Disable auto save on modified with a debounce duration.
168
170
pub fn disable_auto_save ( mut self ) -> Self {
169
171
self . auto_save = None ;
170
172
self
171
173
}
172
174
173
- pub ( crate ) fn build_inner (
174
- mut self ,
175
- mut stores : MutexGuard < ' _ , HashMap < PathBuf , ResourceId > > ,
176
- ) -> crate :: Result < ( Arc < Store < R > > , ResourceId ) > {
175
+ /// Force create a new store even if it already exists.
176
+ pub fn create_new ( mut self ) -> Self {
177
+ self . create_new = true ;
178
+ self
179
+ }
180
+
181
+ pub ( crate ) fn build_inner ( mut self ) -> crate :: Result < ( Arc < Store < R > > , ResourceId ) > {
182
+ let stores = self . app . state :: < StoreState > ( ) . stores . clone ( ) ;
183
+ let mut stores = stores. lock ( ) . unwrap ( ) ;
184
+ if let Some ( rid) = stores. get ( & self . path ) {
185
+ return Ok ( ( self . app . resources_table ( ) . get ( * rid) . unwrap ( ) , * rid) ) ;
186
+ }
187
+
177
188
if stores. contains_key ( & self . path ) {
178
189
return Err ( crate :: Error :: AlreadyExists ( self . path ) ) ;
179
190
}
@@ -185,7 +196,10 @@ impl<R: Runtime> StoreBuilder<R> {
185
196
self . serialize_fn ,
186
197
self . deserialize_fn ,
187
198
) ;
188
- let _ = store_inner. load ( ) ;
199
+
200
+ if !self . create_new {
201
+ let _ = store_inner. load ( ) ;
202
+ }
189
203
190
204
let store = Store {
191
205
auto_save : self . auto_save ,
@@ -200,64 +214,23 @@ impl<R: Runtime> StoreBuilder<R> {
200
214
Ok ( ( store, rid) )
201
215
}
202
216
203
- /// Builds the [`Store`], also see [`build_or_existing`](Self::build_or_existing).
204
- ///
205
- /// This loads the store from disk and put the store in the app's resource table,
206
- /// to remove it from the resource table, call [`Store::close_store`]
207
- ///
208
- /// # Errors
209
- ///
210
- /// If a store with this path is already in the resource table,
211
- /// will return a [`crate::Error::AlreadyExists`]
212
- ///
213
- /// # Examples
214
- /// ```
215
- /// tauri::Builder::default()
216
- /// .plugin(tauri_plugin_store::Builder::default().build())
217
- /// .setup(|app| {
218
- /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").load()?;
219
- /// Ok(())
220
- /// });
221
- /// ```
222
- pub fn create ( self ) -> crate :: Result < Arc < Store < R > > > {
223
- let ( store, _) = self . create_inner ( ) ?;
224
- Ok ( store)
225
- }
226
-
227
- pub ( crate ) fn create_inner ( self ) -> crate :: Result < ( Arc < Store < R > > , ResourceId ) > {
228
- let stores = self . app . state :: < StoreState > ( ) . stores . clone ( ) ;
229
- self . build_inner ( stores. lock ( ) . unwrap ( ) )
230
- }
231
-
232
- /// Get the existing store with the same path or creates a new [`Store`].
217
+ /// Load the existing store with the same path or creates a new [`Store`].
233
218
///
234
219
/// If a store with the same path has already been loaded its instance is returned.
235
220
///
236
- /// See [`create`](Self::create) if you want to force create a new store in the path.
237
- ///
238
221
/// # Examples
239
222
/// ```
240
223
/// tauri::Builder::default()
241
224
/// .plugin(tauri_plugin_store::Builder::default().build())
242
225
/// .setup(|app| {
243
- /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").load ();
226
+ /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").build ();
244
227
/// Ok(())
245
228
/// });
246
229
/// ```
247
- pub fn load ( self ) -> crate :: Result < Arc < Store < R > > > {
248
- let ( store, _) = self . load_inner ( ) ?;
230
+ pub fn build ( self ) -> crate :: Result < Arc < Store < R > > > {
231
+ let ( store, _) = self . build_inner ( ) ?;
249
232
Ok ( store)
250
233
}
251
-
252
- pub ( crate ) fn load_inner ( self ) -> crate :: Result < ( Arc < Store < R > > , ResourceId ) > {
253
- let stores = self . app . state :: < StoreState > ( ) . stores . clone ( ) ;
254
- let stores_ = stores. lock ( ) . unwrap ( ) ;
255
- if let Some ( rid) = stores_. get ( & self . path ) {
256
- return Ok ( ( self . app . resources_table ( ) . get ( * rid) . unwrap ( ) , * rid) ) ;
257
- }
258
-
259
- self . build_inner ( stores_)
260
- }
261
234
}
262
235
263
236
enum AutoSaveMessage {
0 commit comments