rename newOrExisting to load, load to reload

This commit is contained in:
Lucas Nogueira
2024-10-16 15:24:58 -03:00
parent 44016a480f
commit 983bd82212
10 changed files with 58 additions and 59 deletions
+9 -9
View File
@@ -116,7 +116,7 @@ async fn create_store<R: Runtime>(
}
#[tauri::command]
async fn new_or_existing<R: Runtime>(
async fn load<R: Runtime>(
app: AppHandle<R>,
store_state: State<'_, StoreState>,
path: PathBuf,
@@ -132,7 +132,7 @@ async fn new_or_existing<R: Runtime>(
serialize_fn_name,
deserialize_fn_name,
)?;
let (_, rid) = builder.new_or_existing_inner()?;
let (_, rid) = builder.load_inner()?;
Ok(rid)
}
@@ -229,9 +229,9 @@ async fn length<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<usize>
}
#[tauri::command]
async fn load<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
async fn reload<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
let store = app.resources_table().get::<Store<R>>(rid)?;
store.load()
store.reload()
}
#[tauri::command]
@@ -290,7 +290,7 @@ pub trait StoreExt<R: Runtime> {
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = app.store_builder("users.json").auto_save(Duration::from_secs(1)).new_or_existing()?;
/// let store = app.store_builder("users.json").auto_save(Duration::from_secs(1)).load()?;
/// Ok(())
/// });
/// ```
@@ -326,7 +326,7 @@ pub trait StoreExt<R: Runtime> {
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
StoreBuilder::new(self.app_handle(), path).new_or_existing()
StoreBuilder::new(self.app_handle(), path).load()
}
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
@@ -447,7 +447,7 @@ impl Builder {
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").new_or_existing()?;
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").load()?;
/// Ok(())
/// });
/// ```
@@ -455,7 +455,7 @@ impl Builder {
plugin::Builder::new("store")
.invoke_handler(tauri::generate_handler![
create_store,
new_or_existing,
load,
get_store,
close_store,
set,
@@ -468,7 +468,7 @@ impl Builder {
values,
length,
entries,
load,
reload,
save,
])
.setup(move |app_handle, _api| {
+11 -11
View File
@@ -79,7 +79,7 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .defaults(defaults)
/// .new_or_existing()?;
/// .load()?;
/// Ok(())
/// });
/// ```
@@ -97,7 +97,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .default("foo".to_string(), "bar")
/// .new_or_existing()?;
/// .load()?;
/// Ok(())
/// });
/// ```
@@ -119,7 +119,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
/// .new_or_existing()?;
/// .load()?;
/// Ok(())
/// });
/// ```
@@ -137,7 +137,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
/// .new_or_existing()?;
/// .load()?;
/// Ok(())
/// });
/// ```
@@ -155,7 +155,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .auto_save(std::time::Duration::from_millis(100))
/// .new_or_existing()?;
/// .load()?;
/// Ok(())
/// });
/// ```
@@ -215,7 +215,7 @@ impl<R: Runtime> StoreBuilder<R> {
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").new_or_existing()?;
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").load()?;
/// Ok(())
/// });
/// ```
@@ -240,16 +240,16 @@ impl<R: Runtime> StoreBuilder<R> {
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").new_or_existing();
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").load();
/// Ok(())
/// });
/// ```
pub fn new_or_existing(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.new_or_existing_inner()?;
pub fn load(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.load_inner()?;
Ok(store)
}
pub(crate) fn new_or_existing_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
pub(crate) fn load_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
let stores = self.app.state::<StoreState>().stores.clone();
let stores_ = stores.lock().unwrap();
if let Some(rid) = stores_.get(&self.path) {
@@ -519,7 +519,7 @@ impl<R: Runtime> Store<R> {
}
/// Update the store from the on-disk state
pub fn load(&self) -> crate::Result<()> {
pub fn reload(&self) -> crate::Result<()> {
self.store.lock().unwrap().load()
}