tauri-plugin-valtio 2.4.0

Persistent Valtio stores for Tauri
Documentation
// This file was autogenerated and should not be edited manually.
// Check the `codegen` command in the `tauri-store-cli` crate.

use crate::valtio::Valtio;
use tauri::{AppHandle, Manager, Runtime, State, WebviewWindow, Window};
use tauri_store::{Result, Store};

/// Extension for the [`Manager`] trait providing access to the Valtio plugin.
///
/// [`Manager`]: https://docs.rs/tauri/latest/tauri/trait.Manager.html
pub trait ManagerExt<R: Runtime>: Manager<R> {
  /// Returns a handle to the Valtio plugin.
  ///
  /// # Panics
  ///
  /// Panics if the internal [store collection] is not in the [resources table].
  ///
  /// This likely indicates that the method was called before the plugin was properly initialized.
  ///
  /// [store collection]: https://docs.rs/tauri-store/latest/tauri_store/struct.StoreCollection.html
  /// [resources table]: https://docs.rs/tauri/latest/tauri/struct.ResourceTable.html
  fn valtio(&self) -> State<Valtio<R>> {
    self.app_handle().state::<Valtio<R>>()
  }

  /// Calls a closure with a mutable reference to the store with the given id.
  fn with_store<F, T>(&self, id: impl AsRef<str>, f: F) -> Result<T>
  where
    F: FnOnce(&mut Store<R>) -> T,
  {
    self.valtio().with_store(id, f)
  }
}

impl<R: Runtime> ManagerExt<R> for AppHandle<R> {}
impl<R: Runtime> ManagerExt<R> for WebviewWindow<R> {}
impl<R: Runtime> ManagerExt<R> for Window<R> {}