Skip to content

Commit 150e030

Browse files
committed
rename builder fn to build()
1 parent ac91270 commit 150e030

File tree

2 files changed

+35
-84
lines changed

2 files changed

+35
-84
lines changed

plugins/store/src/lib.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async fn create_store<R: Runtime>(
111111
serialize_fn_name,
112112
deserialize_fn_name,
113113
)?;
114-
let (_, rid) = builder.create_inner()?;
114+
let (_, rid) = builder.create_new().build_inner()?;
115115
Ok(rid)
116116
}
117117

@@ -132,7 +132,7 @@ async fn load<R: Runtime>(
132132
serialize_fn_name,
133133
deserialize_fn_name,
134134
)?;
135-
let (_, rid) = builder.load_inner()?;
135+
let (_, rid) = builder.build_inner()?;
136136
Ok(rid)
137137
}
138138

@@ -258,24 +258,6 @@ pub trait StoreExt<R: Runtime> {
258258
/// });
259259
/// ```
260260
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
261-
/// Create a store with default settings.
262-
///
263-
/// If the store is already loaded you must check with [`Self::get_store`] or prefer [`Self::store`]
264-
/// as it will return `Err(Error::AlreadyExists)`.
265-
///
266-
/// # Examples
267-
///
268-
/// ```
269-
/// use tauri_plugin_store::StoreExt;
270-
///
271-
/// tauri::Builder::default()
272-
/// .plugin(tauri_plugin_store::Builder::default().build())
273-
/// .setup(|app| {
274-
/// let store = app.create_store("my-store")?;
275-
/// Ok(())
276-
/// });
277-
/// ```
278-
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
279261
/// Get a store builder.
280262
///
281263
/// The builder can be used to configure the store.
@@ -290,7 +272,7 @@ pub trait StoreExt<R: Runtime> {
290272
/// tauri::Builder::default()
291273
/// .plugin(tauri_plugin_store::Builder::default().build())
292274
/// .setup(|app| {
293-
/// let store = app.store_builder("users.json").auto_save(Duration::from_secs(1)).load()?;
275+
/// let store = app.store_builder("users.json").auto_save(Duration::from_secs(1)).build()?;
294276
/// Ok(())
295277
/// });
296278
/// ```
@@ -326,11 +308,7 @@ pub trait StoreExt<R: Runtime> {
326308

327309
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
328310
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
329-
StoreBuilder::new(self.app_handle(), path).load()
330-
}
331-
332-
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
333-
StoreBuilder::new(self.app_handle(), path).create()
311+
StoreBuilder::new(self.app_handle(), path).build()
334312
}
335313

336314
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R> {
@@ -447,7 +425,7 @@ impl Builder {
447425
/// tauri::Builder::default()
448426
/// .plugin(tauri_plugin_store::Builder::default().build())
449427
/// .setup(|app| {
450-
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").load()?;
428+
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build()?;
451429
/// Ok(())
452430
/// });
453431
/// ```

plugins/store/src/store.rs

Lines changed: 30 additions & 57 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, MutexGuard},
11+
sync::{Arc, Mutex},
1212
time::Duration,
1313
};
1414
use tauri::{path::BaseDirectory, AppHandle, Emitter, Manager, Resource, ResourceId, Runtime};
@@ -38,6 +38,7 @@ pub struct StoreBuilder<R: Runtime> {
3838
serialize_fn: SerializeFn,
3939
deserialize_fn: DeserializeFn,
4040
auto_save: Option<Duration>,
41+
create_new: bool,
4142
}
4243

4344
impl<R: Runtime> StoreBuilder<R> {
@@ -64,6 +65,7 @@ impl<R: Runtime> StoreBuilder<R> {
6465
serialize_fn,
6566
deserialize_fn,
6667
auto_save: Some(Duration::from_millis(100)),
68+
create_new: false,
6769
}
6870
}
6971

@@ -79,7 +81,7 @@ impl<R: Runtime> StoreBuilder<R> {
7981
///
8082
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
8183
/// .defaults(defaults)
82-
/// .load()?;
84+
/// .build()?;
8385
/// Ok(())
8486
/// });
8587
/// ```
@@ -97,7 +99,7 @@ impl<R: Runtime> StoreBuilder<R> {
9799
/// .setup(|app| {
98100
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
99101
/// .default("foo".to_string(), "bar")
100-
/// .load()?;
102+
/// .build()?;
101103
/// Ok(())
102104
/// });
103105
/// ```
@@ -119,7 +121,7 @@ impl<R: Runtime> StoreBuilder<R> {
119121
/// .setup(|app| {
120122
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
121123
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
122-
/// .load()?;
124+
/// .build()?;
123125
/// Ok(())
124126
/// });
125127
/// ```
@@ -137,7 +139,7 @@ impl<R: Runtime> StoreBuilder<R> {
137139
/// .setup(|app| {
138140
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
139141
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
140-
/// .load()?;
142+
/// .build()?;
141143
/// Ok(())
142144
/// });
143145
/// ```
@@ -155,7 +157,7 @@ impl<R: Runtime> StoreBuilder<R> {
155157
/// .setup(|app| {
156158
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
157159
/// .auto_save(std::time::Duration::from_millis(100))
158-
/// .load()?;
160+
/// .build()?;
159161
/// Ok(())
160162
/// });
161163
/// ```
@@ -164,16 +166,25 @@ impl<R: Runtime> StoreBuilder<R> {
164166
self
165167
}
166168

167-
/// Disable auto save on modified with a debounce duration
169+
/// Disable auto save on modified with a debounce duration.
168170
pub fn disable_auto_save(mut self) -> Self {
169171
self.auto_save = None;
170172
self
171173
}
172174

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+
177188
if stores.contains_key(&self.path) {
178189
return Err(crate::Error::AlreadyExists(self.path));
179190
}
@@ -185,7 +196,10 @@ impl<R: Runtime> StoreBuilder<R> {
185196
self.serialize_fn,
186197
self.deserialize_fn,
187198
);
188-
let _ = store_inner.load();
199+
200+
if !self.create_new {
201+
let _ = store_inner.load();
202+
}
189203

190204
let store = Store {
191205
auto_save: self.auto_save,
@@ -200,64 +214,23 @@ impl<R: Runtime> StoreBuilder<R> {
200214
Ok((store, rid))
201215
}
202216

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`].
233218
///
234219
/// If a store with the same path has already been loaded its instance is returned.
235220
///
236-
/// See [`create`](Self::create) if you want to force create a new store in the path.
237-
///
238221
/// # Examples
239222
/// ```
240223
/// tauri::Builder::default()
241224
/// .plugin(tauri_plugin_store::Builder::default().build())
242225
/// .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();
244227
/// Ok(())
245228
/// });
246229
/// ```
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()?;
249232
Ok(store)
250233
}
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-
}
261234
}
262235

263236
enum AutoSaveMessage {

0 commit comments

Comments
 (0)