feat(core): try_state API on the Manager trait (#2341)

This commit is contained in:
Lucas Fernandes Nogueira
2021-08-02 17:01:53 -03:00
committed by GitHub
parent 15566cfd64
commit 84a0e04cbe
3 changed files with 19 additions and 1 deletions

5
.changes/try-state.md Normal file
View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Add `try_state` API to the `Manager` trait.

View File

@@ -308,7 +308,7 @@ 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,
@@ -316,6 +316,14 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
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

View File

@@ -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)
}
}