-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from photovoltex/acquire-state
Add helper to acquire a wrapped state
- Loading branch information
Showing
4 changed files
with
77 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.