forked from yewstack/yew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcomp.rs
298 lines (260 loc) · 7.35 KB
/
vcomp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! This module contains the implementation of a virtual component (`VComp`).
use super::Key;
use crate::html::{BaseComponent, IntoComponent, NodeRef};
use std::any::TypeId;
use std::fmt;
use std::rc::Rc;
#[cfg(any(feature = "ssr", feature = "csr"))]
use crate::html::{AnyScope, Scope};
#[cfg(feature = "csr")]
use crate::dom_bundle::BSubtree;
#[cfg(feature = "hydration")]
use crate::dom_bundle::Fragment;
#[cfg(feature = "csr")]
use crate::html::Scoped;
#[cfg(feature = "csr")]
use web_sys::Element;
#[cfg(feature = "ssr")]
use futures::future::{FutureExt, LocalBoxFuture};
/// A virtual component.
pub struct VComp {
pub(crate) type_id: TypeId,
pub(crate) mountable: Box<dyn Mountable>,
pub(crate) node_ref: NodeRef,
pub(crate) key: Option<Key>,
}
impl fmt::Debug for VComp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VComp")
.field("type_id", &self.type_id)
.field("node_ref", &self.node_ref)
.field("mountable", &"..")
.field("key", &self.key)
.finish()
}
}
impl Clone for VComp {
fn clone(&self) -> Self {
Self {
type_id: self.type_id,
mountable: self.mountable.copy(),
node_ref: self.node_ref.clone(),
key: self.key.clone(),
}
}
}
pub(crate) trait Mountable {
fn copy(&self) -> Box<dyn Mountable>;
#[cfg(feature = "csr")]
fn mount(
self: Box<Self>,
root: &BSubtree,
node_ref: NodeRef,
parent_scope: &AnyScope,
parent: Element,
next_sibling: NodeRef,
) -> Box<dyn Scoped>;
#[cfg(feature = "csr")]
fn reuse(self: Box<Self>, scope: &dyn Scoped, next_sibling: NodeRef);
#[cfg(feature = "ssr")]
fn render_to_string<'a>(
&'a self,
w: &'a mut String,
parent_scope: &'a AnyScope,
hydratable: bool,
) -> LocalBoxFuture<'a, ()>;
#[cfg(feature = "hydration")]
fn hydrate(
self: Box<Self>,
root: BSubtree,
parent_scope: &AnyScope,
parent: Element,
fragment: &mut Fragment,
node_ref: NodeRef,
) -> Box<dyn Scoped>;
}
pub(crate) struct PropsWrapper<COMP: BaseComponent> {
props: Rc<COMP::Properties>,
}
impl<COMP: BaseComponent> PropsWrapper<COMP> {
pub fn new(props: Rc<COMP::Properties>) -> Self {
Self { props }
}
}
impl<COMP: BaseComponent> Mountable for PropsWrapper<COMP> {
fn copy(&self) -> Box<dyn Mountable> {
let wrapper: PropsWrapper<COMP> = PropsWrapper {
props: Rc::clone(&self.props),
};
Box::new(wrapper)
}
#[cfg(feature = "csr")]
fn mount(
self: Box<Self>,
root: &BSubtree,
node_ref: NodeRef,
parent_scope: &AnyScope,
parent: Element,
next_sibling: NodeRef,
) -> Box<dyn Scoped> {
let scope: Scope<COMP> = Scope::new(Some(parent_scope.clone()));
scope.mount_in_place(root.clone(), parent, next_sibling, node_ref, self.props);
Box::new(scope)
}
#[cfg(feature = "csr")]
fn reuse(self: Box<Self>, scope: &dyn Scoped, next_sibling: NodeRef) {
let scope: Scope<COMP> = scope.to_any().downcast::<COMP>();
scope.reuse(self.props, next_sibling);
}
#[cfg(feature = "ssr")]
fn render_to_string<'a>(
&'a self,
w: &'a mut String,
parent_scope: &'a AnyScope,
hydratable: bool,
) -> LocalBoxFuture<'a, ()> {
async move {
let scope: Scope<COMP> = Scope::new(Some(parent_scope.clone()));
scope
.render_to_string(w, self.props.clone(), hydratable)
.await;
}
.boxed_local()
}
#[cfg(feature = "hydration")]
fn hydrate(
self: Box<Self>,
root: BSubtree,
parent_scope: &AnyScope,
parent: Element,
fragment: &mut Fragment,
node_ref: NodeRef,
) -> Box<dyn Scoped> {
let scope: Scope<COMP> = Scope::new(Some(parent_scope.clone()));
scope.hydrate_in_place(root, parent, fragment, node_ref, self.props);
Box::new(scope)
}
}
/// A virtual child component.
pub struct VChild<ICOMP: IntoComponent> {
/// The component properties
pub props: Rc<ICOMP::Properties>,
/// Reference to the mounted node
node_ref: NodeRef,
key: Option<Key>,
}
impl<ICOMP: IntoComponent> Clone for VChild<ICOMP> {
fn clone(&self) -> Self {
VChild {
props: Rc::clone(&self.props),
node_ref: self.node_ref.clone(),
key: self.key.clone(),
}
}
}
impl<ICOMP: IntoComponent> PartialEq for VChild<ICOMP>
where
ICOMP::Properties: PartialEq,
{
fn eq(&self, other: &VChild<ICOMP>) -> bool {
self.props == other.props
}
}
impl<ICOMP> VChild<ICOMP>
where
ICOMP: IntoComponent,
{
/// Creates a child component that can be accessed and modified by its parent.
pub fn new(props: ICOMP::Properties, node_ref: NodeRef, key: Option<Key>) -> Self {
Self {
props: Rc::new(props),
node_ref,
key,
}
}
}
impl<ICOMP> From<VChild<ICOMP>> for VComp
where
ICOMP: IntoComponent,
{
fn from(vchild: VChild<ICOMP>) -> Self {
VComp::new::<ICOMP>(vchild.props, vchild.node_ref, vchild.key)
}
}
impl VComp {
/// Creates a new `VComp` instance.
pub fn new<ICOMP>(props: Rc<ICOMP::Properties>, node_ref: NodeRef, key: Option<Key>) -> Self
where
ICOMP: IntoComponent,
{
VComp {
type_id: TypeId::of::<ICOMP::Component>(),
node_ref,
mountable: Box::new(PropsWrapper::<ICOMP::Component>::new(props)),
key,
}
}
}
impl PartialEq for VComp {
fn eq(&self, other: &VComp) -> bool {
self.type_id == other.type_id
}
}
impl<COMP: BaseComponent> fmt::Debug for VChild<COMP> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VChild<_>")
}
}
#[cfg(feature = "ssr")]
mod feat_ssr {
use super::*;
use crate::html::AnyScope;
impl VComp {
pub(crate) async fn render_to_string(
&self,
w: &mut String,
parent_scope: &AnyScope,
hydratable: bool,
) {
self.mountable
.as_ref()
.render_to_string(w, parent_scope, hydratable)
.await;
}
}
}
#[cfg(all(test, not(target_arch = "wasm32"), feature = "ssr"))]
mod ssr_tests {
use tokio::test;
use crate::prelude::*;
use crate::ServerRenderer;
#[test]
async fn test_props() {
#[derive(PartialEq, Properties, Debug)]
struct ChildProps {
name: String,
}
#[function_component]
fn Child(props: &ChildProps) -> Html {
html! { <div>{"Hello, "}{&props.name}{"!"}</div> }
}
#[function_component]
fn Comp() -> Html {
html! {
<div>
<Child name="Jane" />
<Child name="John" />
<Child name="Josh" />
</div>
}
}
let s = ServerRenderer::<Comp>::new()
.hydratable(false)
.render()
.await;
assert_eq!(
s,
"<div><div>Hello, Jane!</div><div>Hello, John!</div><div>Hello, Josh!</div></div>"
);
}
}