feat(utils): add utility function to safely execute closures with current Leptos owner

This commit is contained in:
ryan 2025-05-09 14:11:15 +03:00
parent a69c51921b
commit e528f9e684
3 changed files with 16 additions and 0 deletions

View file

@ -3,6 +3,8 @@ pub mod components;
pub mod models;
pub mod nostr;
pub mod api;
pub mod utils;
#[cfg(feature = "ssr")]
pub mod db;

13
src/utils/leptos_owner.rs Normal file
View 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
View file

@ -0,0 +1 @@
pub mod leptos_owner;