Skip to content

Commit 84a0e04

Browse files
authored
feat(core): try_state API on the Manager trait (#2341)
1 parent 15566cf commit 84a0e04

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

.changes/try-state.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Add `try_state` API to the `Manager` trait.

core/tauri/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,22 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
308308
self.manager().state().set(state);
309309
}
310310

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

319+
/// Tries to get the managed state for the type `T`. Returns `None` if the type is not managed.
320+
fn try_state<T>(&self) -> Option<State<'_, T>>
321+
where
322+
T: Send + Sync + 'static,
323+
{
324+
self.manager().inner.state.try_get()
325+
}
326+
319327
/// Adds a plugin to the runtime.
320328
fn plugin<P: plugin::Plugin<R> + 'static>(&self, plugin: P) {
321329
self

core/tauri/src/state.rs

+5
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ impl StateManager {
6060
pub fn get<T: Send + Sync + 'static>(&self) -> State<'_, T> {
6161
State(self.0.get())
6262
}
63+
64+
/// Gets the state associated with the specified type.
65+
pub fn try_get<T: Send + Sync + 'static>(&self) -> Option<State<'_, T>> {
66+
self.0.try_get().map(State)
67+
}
6368
}

0 commit comments

Comments
 (0)