Skip to content

Commit

Permalink
feat(core): try_state API on the Manager trait (#2341)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 2, 2021
1 parent 15566cf commit 84a0e04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/try-state.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Add `try_state` API to the `Manager` trait.
10 changes: 9 additions & 1 deletion core/tauri/src/lib.rs
Expand Up @@ -308,14 +308,22 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
self.manager().state().set(state);
}

/// Gets the managed state for the type `T`.
/// Gets the managed state for the type `T`. Panics if the type is not managed.
fn state<T>(&self) -> State<'_, T>
where
T: Send + Sync + 'static,
{
self.manager().inner.state.get()
}

/// Tries to get the managed state for the type `T`. Returns `None` if the type is not managed.
fn try_state<T>(&self) -> Option<State<'_, T>>
where
T: Send + Sync + 'static,
{
self.manager().inner.state.try_get()
}

/// Adds a plugin to the runtime.
fn plugin<P: plugin::Plugin<R> + 'static>(&self, plugin: P) {
self
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/state.rs
Expand Up @@ -60,4 +60,9 @@ impl StateManager {
pub fn get<T: Send + Sync + 'static>(&self) -> State<'_, T> {
State(self.0.get())
}

/// Gets the state associated with the specified type.
pub fn try_get<T: Send + Sync + 'static>(&self) -> Option<State<'_, T>> {
self.0.try_get().map(State)
}
}

0 comments on commit 84a0e04

Please sign in to comment.