From 422233eecf9107602fa9b379689c33bdcc5b0b14 Mon Sep 17 00:00:00 2001 From: Jose Quesada Date: Thu, 15 Dec 2022 12:05:17 -0600 Subject: [PATCH] removed some HydrationCtx things and improved tracing for `DynChild` --- leptos_dom/src/components/dyn_child.rs | 2 - leptos_dom/src/hydration.rs | 12 --- leptos_dom/src/lib.rs | 18 ++-- leptos_dom/src/ssr.rs | 119 +++++++++++++------------ 4 files changed, 70 insertions(+), 81 deletions(-) diff --git a/leptos_dom/src/components/dyn_child.rs b/leptos_dom/src/components/dyn_child.rs index 33a35abc2..9f5a924d2 100644 --- a/leptos_dom/src/components/dyn_child.rs +++ b/leptos_dom/src/components/dyn_child.rs @@ -151,8 +151,6 @@ where move |prev_run: Option<(Option, ScopeDisposer)>| { #[cfg(debug_assertions)] let _guard = span.enter(); - #[cfg(debug_assertions)] - let _guard = trace_span!("DynChild reactive").entered(); let (new_child, disposer) = cx.run_child_scope(|cx| child_fn().into_view(cx)); diff --git a/leptos_dom/src/hydration.rs b/leptos_dom/src/hydration.rs index 3213f16c6..8b5a7cb5e 100644 --- a/leptos_dom/src/hydration.rs +++ b/leptos_dom/src/hydration.rs @@ -56,18 +56,6 @@ impl HydrationCtx { unsafe { ID = id } } - #[cfg(not(all(target_arch = "wasm32", feature = "web")))] - pub(crate) fn set_id(cx: Scope) { - /* let new_id = if let Some(id) = cx.get_hydration_key() { - id + 1 - } else { - 0 - }; */ - let new_id = 0; - println!("setting ID to {new_id}"); - unsafe { ID = new_id }; - } - #[cfg(all(target_arch = "wasm32", feature = "web"))] pub(crate) fn stop_hydrating() { unsafe { diff --git a/leptos_dom/src/lib.rs b/leptos_dom/src/lib.rs index c7f750d47..abe5f5f97 100644 --- a/leptos_dom/src/lib.rs +++ b/leptos_dom/src/lib.rs @@ -6,7 +6,7 @@ //! The DOM implementation for `leptos`. #[cfg_attr(debug_assertions, macro_use)] -extern crate tracing; +pub extern crate tracing; mod components; mod events; @@ -19,7 +19,6 @@ mod node_ref; mod ssr; mod transparent; - use cfg_if::cfg_if; pub use components::*; pub use events::typed as ev; @@ -29,7 +28,7 @@ pub use hydration::HydrationCtx; pub use js_sys; use leptos_reactive::Scope; pub use logging::*; -pub use macro_helpers::{IntoClass, IntoAttribute, IntoProperty}; +pub use macro_helpers::{IntoAttribute, IntoClass, IntoProperty}; pub use node_ref::*; #[cfg(not(all(target_arch = "wasm32", feature = "web")))] use smallvec::SmallVec; @@ -109,7 +108,7 @@ where { #[cfg_attr( debug_assertions, - instrument(level = "trace", name = "Fn() -> N", skip_all) + instrument(level = "trace", name = "Fn() -> impl IntoView", skip_all) )] fn into_view(self, cx: Scope) -> View { DynChild::new(self).into_view(cx) @@ -206,7 +205,7 @@ impl Element { attrs, children, id, - prerendered + prerendered, } = self; let element = AnyElement { name, is_void, id }; @@ -216,7 +215,7 @@ impl Element { element, attrs, children: children.into_iter().collect(), - prerendered + prerendered, } } } @@ -255,14 +254,17 @@ impl Element { #[cfg(not(all(target_arch = "wasm32", feature = "web")))] #[track_caller] - fn from_html(el: El, html: impl Into>) -> Self { + fn from_html( + el: El, + html: impl Into>, + ) -> Self { Self { name: el.name(), is_void: el.is_void(), attrs: Default::default(), children: Default::default(), id: el.hydration_id(), - prerendered: Some(html.into()) + prerendered: Some(html.into()), } } } diff --git a/leptos_dom/src/ssr.rs b/leptos_dom/src/ssr.rs index 10d290317..ed46d08f0 100644 --- a/leptos_dom/src/ssr.rs +++ b/leptos_dom/src/ssr.rs @@ -2,12 +2,11 @@ use crate::{CoreComponent, HydrationCtx, IntoView, View}; use cfg_if::cfg_if; -use futures::{Stream, StreamExt, stream::FuturesUnordered}; +use futures::{stream::FuturesUnordered, Stream, StreamExt}; use itertools::Itertools; -use std::borrow::Cow; use leptos_reactive::*; +use std::borrow::Cow; -#[cfg(not(all(target_arch = "wasm32", feature = "web")))] /// Renders the given function to a static HTML string. /// /// ``` @@ -23,12 +22,17 @@ pub fn render_to_string(f: F) -> String where F: FnOnce(Scope) -> N + 'static, N: IntoView, - { - let runtime = leptos_reactive::create_runtime(); - HydrationCtx::reset_id(); - let html = leptos_reactive::run_scope(runtime, |cx| f(cx).into_view(cx).render_to_string(cx)); - runtime.dispose(); - html.into_owned() +{ + let runtime = leptos_reactive::create_runtime(); + HydrationCtx::reset_id(); + + let html = leptos_reactive::run_scope(runtime, |cx| { + f(cx).into_view(cx).render_to_string(cx) + }); + + runtime.dispose(); + + html.into_owned() } /// Renders a function to a stream of HTML strings. @@ -43,7 +47,9 @@ where /// it is waiting for a resource to resolve from the server, it doesn't run it initially. /// 3) HTML fragments to replace each `` fallback with its actual data as the resources /// read under that `` resolve. -pub fn render_to_stream(view: impl FnOnce(Scope) -> View + 'static) -> impl Stream { +pub fn render_to_stream( + view: impl FnOnce(Scope) -> View + 'static, +) -> impl Stream { render_to_stream_with_prefix(view, |_| "".into()) } @@ -63,44 +69,47 @@ pub fn render_to_stream(view: impl FnOnce(Scope) -> View + 'static) -> impl Stre /// read under that `` resolve. pub fn render_to_stream_with_prefix( view: impl FnOnce(Scope) -> View + 'static, - prefix: impl FnOnce(Scope) -> Cow<'static, str> + 'static + prefix: impl FnOnce(Scope) -> Cow<'static, str> + 'static, ) -> impl Stream { HydrationCtx::reset_id(); - + // create the runtime let runtime = create_runtime(); - let ((shell, prefix, pending_resources, pending_fragments, serializers), _, disposer) = - run_scope_undisposed(runtime, { - move |cx| { - // the actual app body/template code - // this does NOT contain any of the data being loaded asynchronously in resources - let shell = view(cx).render_to_string(cx); + let ( + (shell, prefix, pending_resources, pending_fragments, serializers), + _, + disposer, + ) = run_scope_undisposed(runtime, { + move |cx| { + // the actual app body/template code + // this does NOT contain any of the data being loaded asynchronously in resources + let shell = view(cx).render_to_string(cx); - let resources = cx.all_resources(); - let pending_resources = serde_json::to_string(&resources).unwrap(); - let prefix = prefix(cx); + let resources = cx.all_resources(); + let pending_resources = serde_json::to_string(&resources).unwrap(); + let prefix = prefix(cx); - ( - shell, - prefix, - pending_resources, - cx.pending_fragments(), - cx.serialization_resolvers(), - ) - } - }); - - let fragments = FuturesUnordered::new(); - for (fragment_id, fut) in pending_fragments { - fragments.push(async move { (fragment_id, fut.await) }) + ( + shell, + prefix, + pending_resources, + cx.pending_fragments(), + cx.serialization_resolvers(), + ) } + }); + + let fragments = FuturesUnordered::new(); + for (fragment_id, fut) in pending_fragments { + fragments.push(async move { (fragment_id, fut.await) }) + } // resources and fragments // stream HTML for each as it resolves let fragments = fragments.map(|(fragment_id, html)| { - format!( - r#" + format!( + r#" "# - ) + ) }); // stream data for each Resource as it resolves - let resources = - serializers.map(|(id, json)| { - let id = serde_json::to_string(&id).unwrap(); - format!( - r#""#, - ) + ) }); // HTML for the view function and script to store resources futures::stream::once(async move { - format!( - r#" + format!( + r#" {prefix} {shell} "# - ) + ) }) // TODO these should be combined again in a way that chains them appropriately // such that individual resources can resolve before all fragments are done @@ -145,23 +153,16 @@ pub fn render_to_stream_with_prefix( .chain(resources) // dispose of Scope and Runtime .chain(futures::stream::once(async move { - disposer.dispose(); - runtime.dispose(); - Default::default() + disposer.dispose(); + runtime.dispose(); + Default::default() })) } impl View { /// Consumes the node and renders it into an HTML string. pub fn render_to_string(self, cx: Scope) -> Cow<'static, str> { - //cx.set_hydration_key(HydrationCtx::current_id()); - HydrationCtx::set_id(cx); - - let s = self.render_to_string_helper(); - - //cx.set_hydration_key(HydrationCtx::current_id()); - - s + self.render_to_string_helper() } pub(crate) fn render_to_string_helper(self) -> Cow<'static, str> { @@ -299,7 +300,7 @@ impl View { } }) .join(""); - + if el.is_void { format!("<{tag_name}{attrs}/>").into() } else { @@ -308,7 +309,7 @@ impl View { .into_iter() .map(|node| node.render_to_string_helper()) .join(""); - + format!("<{tag_name}{attrs}>{children}").into() } }