diff --git a/src/command/bindings.rs b/src/command/bindings.rs index 9b43850..8c4a4c7 100644 --- a/src/command/bindings.rs +++ b/src/command/bindings.rs @@ -15,6 +15,8 @@ extern "C" { /// [Events](https://tauri.app/v1/guides/features/events) #[cfg(feature = "event")] #[doc(cfg(feature = "event"))] + // for some reason this doc comment is seen as unused... + #[allow(unused_doc_comments)] #[wasm_bindgen(catch, js_namespace = ["window", "__TAURI__", "event"])] pub async fn listen( event: &str, diff --git a/src/event/emit.rs b/src/event/emit.rs index 5e8fb58..57e538f 100644 --- a/src/event/emit.rs +++ b/src/event/emit.rs @@ -4,6 +4,11 @@ use super::Field; #[cfg(doc)] use super::Listen; +#[cfg(feature = "initial_value")] +#[doc(cfg(feature = "initial_value"))] +/// A mod containing functions to acquire a wrapped state manged by tauri +pub mod state_helper; + /// The trait which needs to be implemented for a [Field] /// /// Conditionally changes between [Listen] and [Emit] or [ManagedEmit] @@ -22,27 +27,22 @@ pub trait Parent = ManagedEmit; #[cfg(feature = "initial_value")] #[doc(cfg(feature = "initial_value"))] pub trait ManagedEmit: Emit -where - Self: 'static, + where + Self: 'static + Send + Sync, { /// Gets the value of a [Field] from [AppHandle] /// - /// The default implementation acquires [Self] directly. Override the provided - /// method when [Self] is not directly managed. For example, this could be the - /// case when the [interior mutability](https://doc.rust-lang.org/reference/interior-mutability.html) + /// The default implementation acquires [Self] directly using [state_helper::directly]. + /// Override the provided method when [Self] is not directly managed. For example, + /// this could be the case when the [interior mutability](https://doc.rust-lang.org/reference/interior-mutability.html) /// pattern is used to allow mutation of [Self] while being managed by tauri. + /// + /// Default state acquiring is provided via [state_helper]. fn get_value>( handle: &AppHandle, - get_field_value: impl Fn(&Self) -> F::Type, - ) -> Option - where - Self: Send + Sync, - { - use tauri::Manager; - - let state = handle.try_state::()?; - let state = get_field_value(&state); - Some(state) + f: impl Fn(&Self) -> F::Type, + ) -> Option { + state_helper::directly::(handle, f) } } @@ -60,7 +60,7 @@ pub trait Emit: Sized { /// foo: String, /// pub bar: bool, /// } - /// + /// /// #[cfg(feature = "initial_value")] /// impl tauri_interop::event::ManagedEmit for Test {} /// @@ -88,7 +88,7 @@ pub trait Emit: Sized { /// /// #[cfg(feature = "initial_value")] /// impl tauri_interop::event::ManagedEmit for Test {} - /// + /// /// #[tauri_interop::command] /// fn emit_bar(handle: TauriAppHandle) { /// Test::default().emit::(&handle).expect("emitting failed"); @@ -97,8 +97,8 @@ pub trait Emit: Sized { /// fn main() {} /// ``` fn emit>(&self, handle: &AppHandle) -> Result<(), Error> - where - Self: Parent; + where + Self: Parent; /// Update a single field and emit it afterward /// @@ -129,6 +129,6 @@ pub trait Emit: Sized { handle: &AppHandle, field: F::Type, ) -> Result<(), Error> - where - Self: Parent; + where + Self: Parent; } diff --git a/src/event/emit/state_helper.rs b/src/event/emit/state_helper.rs new file mode 100644 index 0000000..7710891 --- /dev/null +++ b/src/event/emit/state_helper.rs @@ -0,0 +1,50 @@ +use std::sync::{Mutex, RwLock}; +use super::*; + +/// Acquires the state directly +/// +/// Default usage when [ManagedEmit::get_value] isn't overridden. +pub fn directly>( + handle: &AppHandle, + f: impl Fn(&P) -> F::Type, +) -> Option { + use tauri::Manager; + + let state = handle.try_state::

()?; + Some(f(&state)) +} + +/// Acquires the state wrapped in an [Option] +pub fn option>( + handle: &AppHandle, + f: impl Fn(&P) -> F::Type, +) -> Option { + use tauri::Manager; + + let state = handle.try_state::>()?; + Some(f(state.as_ref()?)) +} + +/// Acquires the state wrapped in an [RwLock] +pub fn rwlock>( + handle: &AppHandle, + f: impl Fn(&P) -> F::Type, +) -> Option { + use tauri::Manager; + + let state = handle.try_state::>()?; + let state = state.read().ok()?; + Some(f(&state)) +} + +/// Acquires the state wrapped in a [Mutex] +pub fn mutex>( + handle: &AppHandle, + f: impl Fn(&P) -> F::Type, +) -> Option { + use tauri::Manager; + + let state = handle.try_state::>()?; + let state = state.lock().ok()?; + Some(f(&state)) +} diff --git a/test-project/Cargo.lock b/test-project/Cargo.lock index d08814e..fed715d 100644 --- a/test-project/Cargo.lock +++ b/test-project/Cargo.lock @@ -3483,7 +3483,7 @@ dependencies = [ [[package]] name = "tauri-interop" -version = "2.1.4" +version = "2.1.5" dependencies = [ "js-sys", "leptos", @@ -3499,7 +3499,9 @@ dependencies = [ [[package]] name = "tauri-interop-macro" -version = "2.1.3" +version = "2.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c90591eecc23b233a58d2f2161bf2321fbcda56651f67c7f83726d831de32d9" dependencies = [ "convert_case 0.6.0", "lazy_static",