Skip to content

Commit e81f07c

Browse files
committed
Collectable.
1 parent 88e1567 commit e81f07c

File tree

3 files changed

+93
-12
lines changed

3 files changed

+93
-12
lines changed

packages/yew/src/html/component/scope.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ mod feat_ssr {
204204
ComponentRenderState, CreateRunner, DestroyRunner, RenderRunner,
205205
};
206206

207+
use crate::virtual_dom::Collectable;
208+
207209
impl<COMP: BaseComponent> Scope<COMP> {
208210
pub(crate) async fn render_to_string(
209211
self,
@@ -227,12 +229,14 @@ mod feat_ssr {
227229
);
228230
scheduler::start();
229231

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

234-
#[cfg(not(debug_assertions))]
235-
w.push_str("<!--<[]>-->");
235+
#[cfg(not(debug_assertions))]
236+
let collectable = Collectable::Component;
237+
238+
if hydratable {
239+
collectable.write_open_tag(w);
236240
}
237241

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

243247
if hydratable {
244-
#[cfg(debug_assertions)]
245-
w.push_str(&format!("<!--</[{}]>-->", std::any::type_name::<COMP>()));
246-
247-
#[cfg(not(debug_assertions))]
248-
w.push_str("<!--</[]>-->");
248+
collectable.write_close_tag(w);
249249
}
250250

251251
scheduler::push_component_destroy(Box::new(DestroyRunner {

packages/yew/src/virtual_dom/mod.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,84 @@ mod tests_attr_value {
179179
}
180180
}
181181

182+
#[cfg(feature = "ssr")] // & feature = "hydration"
183+
mod feat_ssr_hydration {
184+
/// A collectable.
185+
///
186+
/// This indicates a kind that can be collected from fragment to be processed at a later time
187+
pub(crate) enum Collectable {
188+
#[cfg(debug_assertions)]
189+
Component(&'static str),
190+
#[cfg(not(debug_assertions))]
191+
Component,
192+
Suspense,
193+
}
194+
195+
impl Collectable {
196+
pub fn open_start_mark(&self) -> &'static str {
197+
match self {
198+
#[cfg(debug_assertions)]
199+
Self::Component(_) => "<[",
200+
#[cfg(not(debug_assertions))]
201+
Self::Component => "<[",
202+
Self::Suspense => "<?",
203+
}
204+
}
205+
pub fn close_start_mark(&self) -> &'static str {
206+
match self {
207+
#[cfg(debug_assertions)]
208+
Self::Component(_) => "</[",
209+
#[cfg(not(debug_assertions))]
210+
Self::Component => "</[",
211+
Self::Suspense => "</?",
212+
}
213+
}
214+
215+
pub fn end_mark(&self) -> &'static str {
216+
match self {
217+
#[cfg(debug_assertions)]
218+
Self::Component(_) => "]>",
219+
#[cfg(not(debug_assertions))]
220+
Self::Component => "]>",
221+
Self::Suspense => ">",
222+
}
223+
}
224+
225+
#[cfg(feature = "ssr")]
226+
pub fn write_open_tag(&self, w: &mut String) {
227+
w.push_str("<!--");
228+
w.push_str(self.open_start_mark());
229+
230+
#[cfg(debug_assertions)]
231+
match self {
232+
Self::Component(type_name) => w.push_str(type_name),
233+
Self::Suspense => {}
234+
}
235+
236+
w.push_str(self.end_mark());
237+
w.push_str("-->");
238+
}
239+
240+
#[cfg(feature = "ssr")]
241+
pub fn write_close_tag(&self, w: &mut String) {
242+
w.push_str("<!--");
243+
w.push_str(self.close_start_mark());
244+
245+
#[cfg(debug_assertions)]
246+
match self {
247+
Self::Component(type_name) => w.push_str(type_name),
248+
Self::Suspense => {}
249+
}
250+
251+
w.push_str(self.end_mark());
252+
w.push_str("-->");
253+
}
254+
}
255+
}
256+
257+
#[cfg(feature = "ssr")]
258+
pub(crate) use feat_ssr_hydration::*;
259+
182260
/// A collection of attributes for an element
183261
#[derive(PartialEq, Eq, Clone, Debug)]
184262
pub enum Attributes {

packages/yew/src/virtual_dom/vsuspense.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl VSuspense {
2828
mod feat_ssr {
2929
use super::*;
3030
use crate::html::AnyScope;
31+
use crate::virtual_dom::Collectable;
3132

3233
impl VSuspense {
3334
pub(crate) async fn render_to_string(
@@ -36,8 +37,10 @@ mod feat_ssr {
3637
parent_scope: &AnyScope,
3738
hydratable: bool,
3839
) {
40+
let collectable = Collectable::Suspense;
41+
3942
if hydratable {
40-
w.push_str("<!--<?>-->");
43+
collectable.write_open_tag(w);
4144
}
4245

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

4851
if hydratable {
49-
w.push_str("<!--</?>-->");
52+
collectable.write_close_tag(w);
5053
}
5154
}
5255
}

0 commit comments

Comments
 (0)