mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-29 12:06:01 +02:00
make api consistent with the JS implementation, add examples
This commit is contained in:
+83
-11
@@ -112,7 +112,7 @@ async fn create_store<R: Runtime>(
|
||||
serialize_fn_name,
|
||||
deserialize_fn_name,
|
||||
)?;
|
||||
let (_, rid) = builder.build_inner()?;
|
||||
let (_, rid) = builder.create_inner()?;
|
||||
Ok(rid)
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ async fn create_or_load<R: Runtime>(
|
||||
serialize_fn_name,
|
||||
deserialize_fn_name,
|
||||
)?;
|
||||
let (_, rid) = builder.build_or_existing_inner()?;
|
||||
let (_, rid) = builder.create_or_load_inner()?;
|
||||
Ok(rid)
|
||||
}
|
||||
|
||||
@@ -242,23 +242,95 @@ async fn save<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
|
||||
}
|
||||
|
||||
pub trait StoreExt<R: Runtime> {
|
||||
/// Create a store or get an existing store with default settings at path
|
||||
/// Create a store or load an existing store with default settings at the given path.
|
||||
///
|
||||
/// If the store is already loaded, its instance is automatically returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tauri_plugin_store::StoreExt;
|
||||
///
|
||||
/// tauri::Builder::default()
|
||||
/// .plugin(tauri_plugin_store::Builder::default().build())
|
||||
/// .setup(|app| {
|
||||
/// let store = app.store("my-store")?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
|
||||
/// Create a store with default settings
|
||||
/// Create a store with default settings.
|
||||
///
|
||||
/// If the store is already loaded you must check with [`Self::get_store`] or prefer [`Self::store`]
|
||||
/// as it will return `Err(Error::AlreadyExists)`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tauri_plugin_store::StoreExt;
|
||||
///
|
||||
/// tauri::Builder::default()
|
||||
/// .plugin(tauri_plugin_store::Builder::default().build())
|
||||
/// .setup(|app| {
|
||||
/// let store = app.create_store("my-store")?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
|
||||
/// Get a store builder
|
||||
/// Get a store builder.
|
||||
///
|
||||
/// The builder can be used to configure the store.
|
||||
/// To use the default settings see [`Self::store`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tauri_plugin_store::StoreExt;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// 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)).create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R>;
|
||||
/// Get an existing store
|
||||
/// Get a handle of an already loaded store.
|
||||
///
|
||||
/// If the store is not loaded or does not exist, it returns `None`.
|
||||
/// In this case, you should initialize it with [`Self::store`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tauri_plugin_store::StoreExt;
|
||||
///
|
||||
/// tauri::Builder::default()
|
||||
/// .plugin(tauri_plugin_store::Builder::default().build())
|
||||
/// .setup(|app| {
|
||||
/// let store = if let Some(s) = app.get_store("store.json") {
|
||||
/// s
|
||||
/// } else {
|
||||
/// app.store("store.json")?
|
||||
/// };
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
fn get_store(&self, path: impl AsRef<Path>) -> Option<Arc<Store<R>>>;
|
||||
}
|
||||
|
||||
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).build_or_existing()
|
||||
let path = path.as_ref();
|
||||
if let Some(store) = self.get_store(path) {
|
||||
return Ok(store);
|
||||
}
|
||||
StoreBuilder::new(self.app_handle(), path).create_or_load()
|
||||
}
|
||||
|
||||
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
|
||||
StoreBuilder::new(self.app_handle(), path).build()
|
||||
StoreBuilder::new(self.app_handle(), path).create()
|
||||
}
|
||||
|
||||
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R> {
|
||||
@@ -327,7 +399,7 @@ impl<R: Runtime> Builder<R> {
|
||||
/// tauri_plugin_store::Builder::default()
|
||||
/// .register_serialize_fn("no-pretty-json".to_owned(), no_pretty_json)
|
||||
/// .build(),
|
||||
/// )
|
||||
/// );
|
||||
/// ```
|
||||
pub fn register_serialize_fn(mut self, name: String, serialize_fn: SerializeFn) -> Self {
|
||||
self.serialize_fns.insert(name, serialize_fn);
|
||||
@@ -356,7 +428,7 @@ impl<R: Runtime> Builder<R> {
|
||||
/// tauri_plugin_store::Builder::default()
|
||||
/// .default_serialize_fn(no_pretty_json)
|
||||
/// .build(),
|
||||
/// )
|
||||
/// );
|
||||
/// ```
|
||||
pub fn default_serialize_fn(mut self, serialize_fn: SerializeFn) -> Self {
|
||||
self.default_serialize = serialize_fn;
|
||||
@@ -377,7 +449,7 @@ impl<R: Runtime> Builder<R> {
|
||||
/// tauri::Builder::default()
|
||||
/// .plugin(tauri_plugin_store::Builder::default().build())
|
||||
/// .setup(|app| {
|
||||
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build()?;
|
||||
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
|
||||
+22
-33
@@ -38,7 +38,6 @@ pub struct StoreBuilder<R: Runtime> {
|
||||
serialize_fn: SerializeFn,
|
||||
deserialize_fn: DeserializeFn,
|
||||
auto_save: Option<Duration>,
|
||||
load_on_build: bool,
|
||||
}
|
||||
|
||||
impl<R: Runtime> StoreBuilder<R> {
|
||||
@@ -65,7 +64,6 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
serialize_fn,
|
||||
deserialize_fn,
|
||||
auto_save: Some(Duration::from_millis(100)),
|
||||
load_on_build: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +79,7 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
///
|
||||
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
|
||||
/// .defaults(defaults)
|
||||
/// .build()?;
|
||||
/// .create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
@@ -99,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")
|
||||
/// .build()?;
|
||||
/// .create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
@@ -121,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))
|
||||
/// .build()?;
|
||||
/// .create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
@@ -139,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))
|
||||
/// .build()?;
|
||||
/// .create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
@@ -157,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))
|
||||
/// .build()?;
|
||||
/// .create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
@@ -172,13 +170,7 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Skip loading the store on build
|
||||
pub fn skip_initial_load(mut self) -> Self {
|
||||
self.load_on_build = false;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
|
||||
pub(crate) fn build_inner(mut self, load: bool) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
|
||||
let state = self.app.state::<StoreState>();
|
||||
let mut stores = state.stores.lock().unwrap();
|
||||
|
||||
@@ -193,7 +185,7 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
self.serialize_fn,
|
||||
self.deserialize_fn,
|
||||
);
|
||||
if self.load_on_build {
|
||||
if load {
|
||||
let _ = store_inner.load();
|
||||
}
|
||||
|
||||
@@ -210,17 +202,6 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
Ok((store, rid))
|
||||
}
|
||||
|
||||
pub(crate) fn build_or_existing_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
|
||||
{
|
||||
let state = self.app.state::<StoreState>();
|
||||
let stores = state.stores.lock().unwrap();
|
||||
if let Some(rid) = stores.get(&self.path) {
|
||||
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
|
||||
}
|
||||
}
|
||||
self.build_inner()
|
||||
}
|
||||
|
||||
/// Builds the [`Store`], also see [`build_or_existing`](Self::build_or_existing).
|
||||
///
|
||||
/// This loads the store from disk and put the store in the app's resource table,
|
||||
@@ -236,30 +217,38 @@ 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").build()?;
|
||||
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").create_or_load()?;
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
pub fn build(self) -> crate::Result<Arc<Store<R>>> {
|
||||
let (store, _) = self.build_inner()?;
|
||||
pub fn create(self) -> crate::Result<Arc<Store<R>>> {
|
||||
let (store, _) = self.create_inner()?;
|
||||
Ok(store)
|
||||
}
|
||||
|
||||
/// Get the existing store with the same path or builds a new [`Store`], also see [`build`](Self::build).
|
||||
pub(crate) fn create_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
|
||||
self.build_inner(false)
|
||||
}
|
||||
|
||||
/// Get the existing store with the same path or creates a new [`Store`], also see [`create`](Self::create).
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// tauri::Builder::default()
|
||||
/// .plugin(tauri_plugin_store::Builder::default().build())
|
||||
/// .setup(|app| {
|
||||
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").build_or_existing();
|
||||
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").create_or_load();
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
pub fn build_or_existing(self) -> crate::Result<Arc<Store<R>>> {
|
||||
let (store, _) = self.build_or_existing_inner()?;
|
||||
pub fn create_or_load(self) -> crate::Result<Arc<Store<R>>> {
|
||||
let (store, _) = self.create_or_load_inner()?;
|
||||
Ok(store)
|
||||
}
|
||||
|
||||
pub(crate) fn create_or_load_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
|
||||
self.build_inner(true)
|
||||
}
|
||||
}
|
||||
|
||||
enum AutoSaveMessage {
|
||||
|
||||
Reference in New Issue
Block a user