mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
removed some HydrationCtx things and improved tracing for DynChild
This commit is contained in:
parent
3b99d2d4fd
commit
422233eecf
4 changed files with 70 additions and 81 deletions
|
@ -151,8 +151,6 @@ where
|
|||
move |prev_run: Option<(Option<web_sys::Node>, 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));
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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: IntoElement>(el: El, html: impl Into<Cow<'static, str>>) -> Self {
|
||||
fn from_html<El: IntoElement>(
|
||||
el: El,
|
||||
html: impl Into<Cow<'static, str>>,
|
||||
) -> 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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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, N>(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 `<Suspense/>` fallback with its actual data as the resources
|
||||
/// read under that `<Suspense/>` resolve.
|
||||
pub fn render_to_stream(view: impl FnOnce(Scope) -> View + 'static) -> impl Stream<Item = String> {
|
||||
pub fn render_to_stream(
|
||||
view: impl FnOnce(Scope) -> View + 'static,
|
||||
) -> impl Stream<Item = String> {
|
||||
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 `<Suspense/>` 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<Item = String> {
|
||||
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 <Suspense/> as it resolves
|
||||
let fragments = fragments.map(|(fragment_id, html)| {
|
||||
format!(
|
||||
r#"
|
||||
format!(
|
||||
r#"
|
||||
<template id="{fragment_id}f">{html}</template>
|
||||
<script>
|
||||
var frag = document.getElementById("{fragment_id}");
|
||||
|
@ -108,27 +117,26 @@ pub fn render_to_stream_with_prefix(
|
|||
if(frag) frag.replaceWith(tpl.content.cloneNode(true));
|
||||
</script>
|
||||
"#
|
||||
)
|
||||
)
|
||||
});
|
||||
// stream data for each Resource as it resolves
|
||||
let resources =
|
||||
serializers.map(|(id, json)| {
|
||||
let id = serde_json::to_string(&id).unwrap();
|
||||
format!(
|
||||
r#"<script>
|
||||
let resources = serializers.map(|(id, json)| {
|
||||
let id = serde_json::to_string(&id).unwrap();
|
||||
format!(
|
||||
r#"<script>
|
||||
if(__LEPTOS_RESOURCE_RESOLVERS.get({id})) {{
|
||||
__LEPTOS_RESOURCE_RESOLVERS.get({id})({json:?})
|
||||
}} else {{
|
||||
__LEPTOS_RESOLVED_RESOURCES.set({id}, {json:?});
|
||||
}}
|
||||
</script>"#,
|
||||
)
|
||||
)
|
||||
});
|
||||
|
||||
// HTML for the view function and script to store resources
|
||||
futures::stream::once(async move {
|
||||
format!(
|
||||
r#"
|
||||
format!(
|
||||
r#"
|
||||
{prefix}
|
||||
{shell}
|
||||
<script>
|
||||
|
@ -137,7 +145,7 @@ pub fn render_to_stream_with_prefix(
|
|||
__LEPTOS_RESOURCE_RESOLVERS = new Map();
|
||||
</script>
|
||||
"#
|
||||
)
|
||||
)
|
||||
})
|
||||
// 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> {
|
||||
|
|
Loading…
Reference in a new issue