-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
vnode.rs
202 lines (181 loc) · 5.79 KB
/
vnode.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
use std::cmp::PartialEq;
use std::fmt;
use std::iter::FromIterator;
use web_sys::Node;
use super::{Key, VChild, VComp, VList, VPortal, VSuspense, VTag, VText};
use crate::html::BaseComponent;
/// Bind virtual element to a DOM reference.
#[derive(Clone)]
pub enum VNode {
/// A bind between `VTag` and `Element`.
VTag(Box<VTag>),
/// A bind between `VText` and `TextNode`.
VText(VText),
/// A bind between `VComp` and `Element`.
VComp(VComp),
/// A holder for a list of other nodes.
VList(VList),
/// A portal to another part of the document
VPortal(VPortal),
/// A holder for any `Node` (necessary for replacing node).
VRef(Node),
/// A suspendible document fragment.
VSuspense(VSuspense),
}
impl VNode {
pub fn key(&self) -> Option<&Key> {
match self {
VNode::VComp(vcomp) => vcomp.key.as_ref(),
VNode::VList(vlist) => vlist.key.as_ref(),
VNode::VRef(_) => None,
VNode::VTag(vtag) => vtag.key.as_ref(),
VNode::VText(_) => None,
VNode::VPortal(vportal) => vportal.node.key(),
VNode::VSuspense(vsuspense) => vsuspense.key.as_ref(),
}
}
/// Returns true if the [VNode] has a key.
pub fn has_key(&self) -> bool {
self.key().is_some()
}
}
impl Default for VNode {
fn default() -> Self {
VNode::VList(VList::default())
}
}
impl From<VText> for VNode {
#[inline]
fn from(vtext: VText) -> Self {
VNode::VText(vtext)
}
}
impl From<VList> for VNode {
#[inline]
fn from(vlist: VList) -> Self {
VNode::VList(vlist)
}
}
impl From<VTag> for VNode {
#[inline]
fn from(vtag: VTag) -> Self {
VNode::VTag(Box::new(vtag))
}
}
impl From<VComp> for VNode {
#[inline]
fn from(vcomp: VComp) -> Self {
VNode::VComp(vcomp)
}
}
impl From<VSuspense> for VNode {
#[inline]
fn from(vsuspense: VSuspense) -> Self {
VNode::VSuspense(vsuspense)
}
}
impl From<VPortal> for VNode {
#[inline]
fn from(vportal: VPortal) -> Self {
VNode::VPortal(vportal)
}
}
impl<COMP> From<VChild<COMP>> for VNode
where
COMP: BaseComponent,
{
fn from(vchild: VChild<COMP>) -> Self {
VNode::VComp(VComp::from(vchild))
}
}
impl<T: ToString> From<T> for VNode {
fn from(value: T) -> Self {
VNode::VText(VText::new(value.to_string()))
}
}
impl<A: Into<VNode>> FromIterator<A> for VNode {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
VNode::VList(VList::with_children(
iter.into_iter().map(|n| n.into()).collect(),
None,
))
}
}
impl fmt::Debug for VNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
VNode::VTag(ref vtag) => vtag.fmt(f),
VNode::VText(ref vtext) => vtext.fmt(f),
VNode::VComp(ref vcomp) => vcomp.fmt(f),
VNode::VList(ref vlist) => vlist.fmt(f),
VNode::VRef(ref vref) => write!(f, "VRef ( \"{}\" )", crate::utils::print_node(vref)),
VNode::VPortal(ref vportal) => vportal.fmt(f),
VNode::VSuspense(ref vsuspense) => vsuspense.fmt(f),
}
}
}
impl PartialEq for VNode {
fn eq(&self, other: &VNode) -> bool {
match (self, other) {
(VNode::VTag(a), VNode::VTag(b)) => a == b,
(VNode::VText(a), VNode::VText(b)) => a == b,
(VNode::VList(a), VNode::VList(b)) => a == b,
(VNode::VRef(a), VNode::VRef(b)) => a == b,
// TODO: Need to improve PartialEq for VComp before enabling.
(VNode::VComp(_), VNode::VComp(_)) => false,
_ => false,
}
}
}
#[cfg(feature = "ssr")]
mod feat_ssr {
use futures::future::{FutureExt, LocalBoxFuture};
use super::*;
use crate::html::AnyScope;
use crate::server_renderer::BufWriter;
impl VNode {
pub(crate) fn render_into_stream<'a>(
&'a self,
w: &'a mut BufWriter,
parent_scope: &'a AnyScope,
hydratable: bool,
) -> LocalBoxFuture<'a, ()> {
async fn render_into_stream_(
this: &VNode,
w: &mut BufWriter,
parent_scope: &AnyScope,
hydratable: bool,
) {
match this {
VNode::VTag(vtag) => vtag.render_into_stream(w, parent_scope, hydratable).await,
VNode::VText(vtext) => {
vtext.render_into_stream(w, parent_scope, hydratable).await
}
VNode::VComp(vcomp) => {
vcomp.render_into_stream(w, parent_scope, hydratable).await
}
VNode::VList(vlist) => {
vlist.render_into_stream(w, parent_scope, hydratable).await
}
// We are pretty safe here as it's not possible to get a web_sys::Node without
// DOM support in the first place.
//
// The only exception would be to use `ServerRenderer` in a browser or wasm32
// environment with jsdom present.
VNode::VRef(_) => {
panic!("VRef is not possible to be rendered in to a string.")
}
// Portals are not rendered.
VNode::VPortal(_) => {}
VNode::VSuspense(vsuspense) => {
vsuspense
.render_into_stream(w, parent_scope, hydratable)
.await
}
}
}
async move { render_into_stream_(self, w, parent_scope, hydratable).await }
.boxed_local()
}
}
}