Skip to content

Commit

Permalink
Merge pull request #33 from photovoltex/acquire-state
Browse files Browse the repository at this point in the history
Add helper to acquire a wrapped state
  • Loading branch information
photovoltex authored Mar 24, 2024
2 parents 1d17f01 + 1ee4039 commit 5e9fe28
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/command/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 21 additions & 21 deletions src/event/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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<F: Field<Self>>(
handle: &AppHandle,
get_field_value: impl Fn(&Self) -> F::Type,
) -> Option<F::Type>
where
Self: Send + Sync,
{
use tauri::Manager;

let state = handle.try_state::<Self>()?;
let state = get_field_value(&state);
Some(state)
f: impl Fn(&Self) -> F::Type,
) -> Option<F::Type> {
state_helper::directly::<Self, F>(handle, f)
}
}

Expand All @@ -60,7 +60,7 @@ pub trait Emit: Sized {
/// foo: String,
/// pub bar: bool,
/// }
///
///
/// #[cfg(feature = "initial_value")]
/// impl tauri_interop::event::ManagedEmit for Test {}
///
Expand Down Expand Up @@ -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::<test::FFoo>(&handle).expect("emitting failed");
Expand All @@ -97,8 +97,8 @@ pub trait Emit: Sized {
/// fn main() {}
/// ```
fn emit<F: Field<Self>>(&self, handle: &AppHandle<Wry>) -> Result<(), Error>
where
Self: Parent;
where
Self: Parent;

/// Update a single field and emit it afterward
///
Expand Down Expand Up @@ -129,6 +129,6 @@ pub trait Emit: Sized {
handle: &AppHandle<Wry>,
field: F::Type,
) -> Result<(), Error>
where
Self: Parent;
where
Self: Parent;
}
50 changes: 50 additions & 0 deletions src/event/emit/state_helper.rs
Original file line number Diff line number Diff line change
@@ -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<P: ManagedEmit, F: Field<P>>(
handle: &AppHandle,
f: impl Fn(&P) -> F::Type,
) -> Option<F::Type> {
use tauri::Manager;

let state = handle.try_state::<P>()?;
Some(f(&state))
}

/// Acquires the state wrapped in an [Option]
pub fn option<P: ManagedEmit, F: Field<P>>(
handle: &AppHandle,
f: impl Fn(&P) -> F::Type,
) -> Option<F::Type> {
use tauri::Manager;

let state = handle.try_state::<Option<P>>()?;
Some(f(state.as_ref()?))
}

/// Acquires the state wrapped in an [RwLock]
pub fn rwlock<P: ManagedEmit, F: Field<P>>(
handle: &AppHandle,
f: impl Fn(&P) -> F::Type,
) -> Option<F::Type> {
use tauri::Manager;

let state = handle.try_state::<RwLock<P>>()?;
let state = state.read().ok()?;
Some(f(&state))
}

/// Acquires the state wrapped in a [Mutex]
pub fn mutex<P: ManagedEmit, F: Field<P>>(
handle: &AppHandle,
f: impl Fn(&P) -> F::Type,
) -> Option<F::Type> {
use tauri::Manager;

let state = handle.try_state::<Mutex<P>>()?;
let state = state.lock().ok()?;
Some(f(&state))
}
6 changes: 4 additions & 2 deletions test-project/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e9fe28

Please sign in to comment.