feat(utils): add utility function to safely execute closures with current Leptos owner
This commit is contained in:
parent
a69c51921b
commit
e528f9e684
3 changed files with 16 additions and 0 deletions
|
@ -3,6 +3,8 @@ pub mod components;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod nostr;
|
pub mod nostr;
|
||||||
pub mod api;
|
pub mod api;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
#[cfg(feature = "ssr")]
|
#[cfg(feature = "ssr")]
|
||||||
pub mod db;
|
pub mod db;
|
||||||
|
|
||||||
|
|
13
src/utils/leptos_owner.rs
Normal file
13
src/utils/leptos_owner.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
/// Utility to safely execute a closure with the current Leptos owner.
|
||||||
|
/// If the owner is disposed, logs and returns None.
|
||||||
|
pub fn with_owner_safe<F, R>(log_context: &str, f: F) -> Option<R>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> R,
|
||||||
|
{
|
||||||
|
if let Some(owner) = leptos::Owner::current() {
|
||||||
|
leptos::try_with_owner(owner, f).ok()
|
||||||
|
} else {
|
||||||
|
leptos::logging::log!("[OWNER] No Leptos owner in context: {}", log_context);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
1
src/utils/mod.rs
Normal file
1
src/utils/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod leptos_owner;
|
Loading…
Add table
Reference in a new issue