diff --git a/src/lib.rs b/src/lib.rs index a2a7cab..324301b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/utils/leptos_owner.rs b/src/utils/leptos_owner.rs new file mode 100644 index 0000000..2ccaa49 --- /dev/null +++ b/src/utils/leptos_owner.rs @@ -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(log_context: &str, f: F) -> Option +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 + } +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..0bd1845 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod leptos_owner; \ No newline at end of file