mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-24 21:40:48 +02:00
chore: update documentation
This commit is contained in:
@@ -4,14 +4,17 @@
|
||||
|
||||
use serde::{Serialize, Serializer};
|
||||
|
||||
/// Alias for a [`Result`](std::result::Result) with the error type [`Error`].
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// The error types.
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
/// The store contents could not be serialized by the configured [`SerializeFn`](crate::SerializeFn).
|
||||
#[error("Failed to serialize store. {0}")]
|
||||
Serialize(Box<dyn std::error::Error + Send + Sync>),
|
||||
/// The store contents could not be deserialized by the configured [`DeserializeFn`](crate::DeserializeFn).
|
||||
#[error("Failed to deserialize store. {0}")]
|
||||
Deserialize(Box<dyn std::error::Error + Send + Sync>),
|
||||
/// JSON error.
|
||||
|
||||
@@ -240,6 +240,11 @@ async fn save<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
|
||||
store.save()
|
||||
}
|
||||
|
||||
/// Extension trait to access the store APIs on a [`Manager`] such as `App`, `AppHandle`,
|
||||
/// `WebviewWindow` or `Window`.
|
||||
///
|
||||
/// The plugin must be registered with [`Builder::build`] for these methods to work,
|
||||
/// as they rely on the state it manages.
|
||||
pub trait StoreExt<R: Runtime> {
|
||||
/// Create a store or load an existing store with default settings at the given path.
|
||||
///
|
||||
@@ -336,6 +341,17 @@ fn default_deserialize(
|
||||
serde_json::from_slice(bytes).map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Builder for the store plugin.
|
||||
///
|
||||
/// It is used to register custom serialize and deserialize functions the frontend can select by
|
||||
/// name when loading a store, and to change the functions used by default (pretty printed JSON).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// tauri::Builder::default()
|
||||
/// .plugin(tauri_plugin_store::Builder::default().build());
|
||||
/// ```
|
||||
pub struct Builder {
|
||||
serialize_fns: HashMap<String, SerializeFn>,
|
||||
deserialize_fns: HashMap<String, DeserializeFn>,
|
||||
@@ -355,6 +371,10 @@ impl Default for Builder {
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
/// Creates a new builder using the default serialize and deserialize functions,
|
||||
/// which read and write pretty printed JSON.
|
||||
///
|
||||
/// This is the same as [`Builder::default`].
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
@@ -18,11 +18,25 @@ use tokio::{
|
||||
time::sleep,
|
||||
};
|
||||
|
||||
/// Function used to serialize the store cache to the bytes written to the store file.
|
||||
///
|
||||
/// The default implementation writes pretty printed JSON.
|
||||
pub type SerializeFn =
|
||||
fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
|
||||
/// Function used to deserialize the bytes read from the store file into the store cache.
|
||||
///
|
||||
/// The default implementation parses JSON.
|
||||
pub type DeserializeFn =
|
||||
fn(&[u8]) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>>;
|
||||
|
||||
/// Resolves the path of a store file, relative to the app data directory
|
||||
/// ([`BaseDirectory::AppData`]).
|
||||
///
|
||||
/// This is the path the [`Store`] created with the given `path` reads from and writes to.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the app data directory cannot be resolved.
|
||||
pub fn resolve_store_path<R: Runtime>(
|
||||
app: &AppHandle<R>,
|
||||
path: impl AsRef<Path>,
|
||||
@@ -428,6 +442,15 @@ impl<R: Runtime> std::fmt::Debug for StoreInner<R> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A key-value store, persisted to a file resolved with [`resolve_store_path`].
|
||||
///
|
||||
/// The values are kept in memory and written to disk on [`Store::save`], and also automatically
|
||||
/// after each modification unless auto save has been disabled with
|
||||
/// [`StoreBuilder::disable_auto_save`]. Any pending auto save is applied when the store is dropped.
|
||||
///
|
||||
/// Create or load one with [`StoreExt::store`](crate::StoreExt::store) or [`StoreBuilder`].
|
||||
/// It is a [`Resource`], so it is also reachable from the frontend by its [`ResourceId`];
|
||||
/// closing that resource unregisters the store, meaning the next load creates a new instance.
|
||||
pub struct Store<R: Runtime> {
|
||||
auto_save: Option<Duration>,
|
||||
auto_save_debounce_sender: Arc<Mutex<Option<UnboundedSender<AutoSaveMessage>>>>,
|
||||
|
||||
Reference in New Issue
Block a user