From e528f9e6840a789e4d3243d86e673e9db1c4169c Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 9 May 2025 14:11:15 +0300 Subject: [PATCH] feat(utils): add utility function to safely execute closures with current Leptos owner --- src/lib.rs | 2 ++ src/utils/leptos_owner.rs | 13 +++++++++++++ src/utils/mod.rs | 1 + 3 files changed, 16 insertions(+) create mode 100644 src/utils/leptos_owner.rs create mode 100644 src/utils/mod.rs 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