Skip to content

Commit

Permalink
Collectable.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Mar 25, 2022
1 parent 88e1567 commit e81f07c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
20 changes: 10 additions & 10 deletions packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ mod feat_ssr {
ComponentRenderState, CreateRunner, DestroyRunner, RenderRunner,
};

use crate::virtual_dom::Collectable;

impl<COMP: BaseComponent> Scope<COMP> {
pub(crate) async fn render_to_string(
self,
Expand All @@ -227,12 +229,14 @@ mod feat_ssr {
);
scheduler::start();

if hydratable {
#[cfg(debug_assertions)]
w.push_str(&format!("<!--<[{}]>-->", std::any::type_name::<COMP>()));
#[cfg(debug_assertions)]
let collectable = Collectable::Component(std::any::type_name::<COMP>());

#[cfg(not(debug_assertions))]
w.push_str("<!--<[]>-->");
#[cfg(not(debug_assertions))]
let collectable = Collectable::Component;

if hydratable {
collectable.write_open_tag(w);
}

let html = rx.await.unwrap();
Expand All @@ -241,11 +245,7 @@ mod feat_ssr {
html.render_to_string(w, &self_any_scope, hydratable).await;

if hydratable {
#[cfg(debug_assertions)]
w.push_str(&format!("<!--</[{}]>-->", std::any::type_name::<COMP>()));

#[cfg(not(debug_assertions))]
w.push_str("<!--</[]>-->");
collectable.write_close_tag(w);
}

scheduler::push_component_destroy(Box::new(DestroyRunner {
Expand Down
78 changes: 78 additions & 0 deletions packages/yew/src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,84 @@ mod tests_attr_value {
}
}

#[cfg(feature = "ssr")] // & feature = "hydration"
mod feat_ssr_hydration {
/// A collectable.
///
/// This indicates a kind that can be collected from fragment to be processed at a later time
pub(crate) enum Collectable {
#[cfg(debug_assertions)]
Component(&'static str),
#[cfg(not(debug_assertions))]
Component,
Suspense,
}

impl Collectable {
pub fn open_start_mark(&self) -> &'static str {
match self {
#[cfg(debug_assertions)]
Self::Component(_) => "<[",
#[cfg(not(debug_assertions))]
Self::Component => "<[",
Self::Suspense => "<?",
}
}
pub fn close_start_mark(&self) -> &'static str {
match self {
#[cfg(debug_assertions)]
Self::Component(_) => "</[",
#[cfg(not(debug_assertions))]
Self::Component => "</[",
Self::Suspense => "</?",
}
}

pub fn end_mark(&self) -> &'static str {
match self {
#[cfg(debug_assertions)]
Self::Component(_) => "]>",
#[cfg(not(debug_assertions))]
Self::Component => "]>",
Self::Suspense => ">",
}
}

#[cfg(feature = "ssr")]
pub fn write_open_tag(&self, w: &mut String) {
w.push_str("<!--");
w.push_str(self.open_start_mark());

#[cfg(debug_assertions)]
match self {
Self::Component(type_name) => w.push_str(type_name),
Self::Suspense => {}
}

w.push_str(self.end_mark());
w.push_str("-->");
}

#[cfg(feature = "ssr")]
pub fn write_close_tag(&self, w: &mut String) {
w.push_str("<!--");
w.push_str(self.close_start_mark());

#[cfg(debug_assertions)]
match self {
Self::Component(type_name) => w.push_str(type_name),
Self::Suspense => {}
}

w.push_str(self.end_mark());
w.push_str("-->");
}
}
}

#[cfg(feature = "ssr")]
pub(crate) use feat_ssr_hydration::*;

/// A collection of attributes for an element
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Attributes {
Expand Down
7 changes: 5 additions & 2 deletions packages/yew/src/virtual_dom/vsuspense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl VSuspense {
mod feat_ssr {
use super::*;
use crate::html::AnyScope;
use crate::virtual_dom::Collectable;

impl VSuspense {
pub(crate) async fn render_to_string(
Expand All @@ -36,8 +37,10 @@ mod feat_ssr {
parent_scope: &AnyScope,
hydratable: bool,
) {
let collectable = Collectable::Suspense;

if hydratable {
w.push_str("<!--<?>-->");
collectable.write_open_tag(w);
}

// always render children on the server side.
Expand All @@ -46,7 +49,7 @@ mod feat_ssr {
.await;

if hydratable {
w.push_str("<!--</?>-->");
collectable.write_close_tag(w);
}
}
}
Expand Down

0 comments on commit e81f07c

Please sign in to comment.