From 5bc0d89ce78642dba73d09df642b2cbd67a5f3a0 Mon Sep 17 00:00:00 2001 From: novacrazy Date: Fri, 7 Apr 2023 00:52:35 -0500 Subject: [PATCH] Cleanup Comment::new --- leptos_dom/src/lib.rs | 61 +++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/leptos_dom/src/lib.rs b/leptos_dom/src/lib.rs index aa0f5917a..ee7a252af 100644 --- a/leptos_dom/src/lib.rs +++ b/leptos_dom/src/lib.rs @@ -373,48 +373,53 @@ struct Comment { } impl Comment { + #[inline] fn new( content: impl Into>, id: &HydrationKey, closing: bool, ) -> Self { - let content = content.into(); + Self::new_inner(content.into(), id, closing) + } - #[cfg(not(all(target_arch = "wasm32", feature = "web")))] - { - let _ = id; - let _ = closing; - } + fn new_inner( + content: Cow<'static, str>, + id: &HydrationKey, + closing: bool, + ) -> Self { + cfg_if! { + if #[cfg(not(all(target_arch = "wasm32", feature = "web")))] { + let _ = id; + let _ = closing; - #[cfg(all(target_arch = "wasm32", feature = "web"))] - let node = COMMENT.with(|comment| comment.clone_node().unwrap()); + Self { content } + } else { + let node = COMMENT.with(|comment| comment.clone_node().unwrap()); - #[cfg(all(debug_assertions, target_arch = "wasm32", feature = "web"))] - node.set_text_content(Some(&format!(" {content} "))); + #[cfg(debug_assertions)] + node.set_text_content(Some(&format!(" {content} "))); - #[cfg(all(target_arch = "wasm32", feature = "web"))] - { - if HydrationCtx::is_hydrating() { - let id = HydrationCtx::to_string(id, closing); + if HydrationCtx::is_hydrating() { + let id = HydrationCtx::to_string(id, closing); - if let Some(marker) = hydration::get_marker(&id) { - marker.before_with_node_1(&node).unwrap(); + if let Some(marker) = hydration::get_marker(&id) { + marker.before_with_node_1(&node).unwrap(); - marker.remove(); - } else { - crate::warn!( - "component with id {id} not found, ignoring it for \ - hydration" - ); + marker.remove(); + } else { + crate::warn!( + "component with id {id} not found, ignoring it for \ + hydration" + ); + } + } + + Self { + node, + content, } } } - - Self { - #[cfg(all(target_arch = "wasm32", feature = "web"))] - node, - content, - } } }