-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
bsuspense.rs
288 lines (257 loc) · 9.71 KB
/
bsuspense.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
//! This module contains the bundle version of a supsense [BSuspense]
use gloo::utils::document;
use web_sys::Element;
#[cfg(feature = "hydration")]
use super::Fragment;
use super::{BNode, BSubtree, Reconcilable, ReconcileTarget};
use crate::html::AnyScope;
use crate::virtual_dom::{Key, VSuspense};
use crate::NodeRef;
#[derive(Debug)]
enum Fallback {
/// Suspense Fallback with fallback being rendered as placeholder.
Bundle(BNode),
/// Suspense Fallback with Hydration Fragment being rendered as placeholder.
#[cfg(feature = "hydration")]
Fragment(Fragment),
}
/// The bundle implementation to [VSuspense]
#[derive(Debug)]
pub(super) struct BSuspense {
children_bundle: BNode,
/// The supsense is suspended if fallback contains [Some] bundle
fallback: Option<Fallback>,
detached_parent: Element,
key: Option<Key>,
}
impl BSuspense {
/// Get the key of the underlying suspense
pub fn key(&self) -> Option<&Key> {
self.key.as_ref()
}
}
impl ReconcileTarget for BSuspense {
fn detach(self, root: &BSubtree, parent: &Element, parent_to_detach: bool) {
match self.fallback {
Some(m) => {
match m {
Fallback::Bundle(bundle) => {
bundle.detach(root, parent, parent_to_detach);
}
#[cfg(feature = "hydration")]
Fallback::Fragment(fragment) => {
fragment.detach(root, parent, parent_to_detach);
}
}
self.children_bundle
.detach(root, &self.detached_parent, false);
}
None => {
self.children_bundle.detach(root, parent, parent_to_detach);
}
}
}
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) -> NodeRef {
match self.fallback.as_ref() {
Some(Fallback::Bundle(bundle)) => bundle.shift(next_parent, next_sibling),
#[cfg(feature = "hydration")]
Some(Fallback::Fragment(fragment)) => fragment.shift(next_parent, next_sibling),
None => self.children_bundle.shift(next_parent, next_sibling),
}
}
}
impl Reconcilable for VSuspense {
type Bundle = BSuspense;
fn attach(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
) -> (NodeRef, Self::Bundle) {
let VSuspense {
children,
fallback,
suspended,
key,
} = self;
let detached_parent = document()
.create_element("div")
.expect("failed to create detached element");
// When it's suspended, we render children into an element that is detached from the dom
// tree while rendering fallback UI into the original place where children resides in.
if suspended {
let (_child_ref, children_bundle) =
children.attach(root, parent_scope, &detached_parent, NodeRef::default());
let (fallback_ref, fallback) =
fallback.attach(root, parent_scope, parent, next_sibling);
(
fallback_ref,
BSuspense {
children_bundle,
fallback: Some(Fallback::Bundle(fallback)),
detached_parent,
key,
},
)
} else {
let (child_ref, children_bundle) =
children.attach(root, parent_scope, parent, next_sibling);
(
child_ref,
BSuspense {
children_bundle,
fallback: None,
detached_parent,
key,
},
)
}
}
fn reconcile_node(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
bundle: &mut BNode,
) -> NodeRef {
match bundle {
// We only preserve the child state if they are the same suspense.
BNode::Suspense(m) if m.key == self.key => {
self.reconcile(root, parent_scope, parent, next_sibling, m)
}
_ => self.replace(root, parent_scope, parent, next_sibling, bundle),
}
}
fn reconcile(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
suspense: &mut Self::Bundle,
) -> NodeRef {
let VSuspense {
children,
fallback: vfallback,
suspended,
key: _,
} = self;
let children_bundle = &mut suspense.children_bundle;
// no need to update key & detached_parent
// When it's suspended, we render children into an element that is detached from the dom
// tree while rendering fallback UI into the original place where children resides in.
match (suspended, &mut suspense.fallback) {
// Both suspended, reconcile children into detached_parent, fallback into the DOM
(true, Some(fallback)) => {
children.reconcile_node(
root,
parent_scope,
&suspense.detached_parent,
NodeRef::default(),
children_bundle,
);
match fallback {
Fallback::Bundle(bundle) => {
vfallback.reconcile_node(root, parent_scope, parent, next_sibling, bundle)
}
#[cfg(feature = "hydration")]
Fallback::Fragment(fragment) => {
let node_ref = NodeRef::default();
match fragment.front().cloned() {
Some(m) => node_ref.set(Some(m)),
None => node_ref.link(next_sibling),
}
node_ref
}
}
}
// Not suspended, just reconcile the children into the DOM
(false, None) => {
children.reconcile_node(root, parent_scope, parent, next_sibling, children_bundle)
}
// Freshly suspended. Shift children into the detached parent, then add fallback to the
// DOM
(true, None) => {
children_bundle.shift(&suspense.detached_parent, NodeRef::default());
children.reconcile_node(
root,
parent_scope,
&suspense.detached_parent,
NodeRef::default(),
children_bundle,
);
// first render of fallback
let (fallback_ref, fallback) =
vfallback.attach(root, parent_scope, parent, next_sibling);
suspense.fallback = Some(Fallback::Bundle(fallback));
fallback_ref
}
// Freshly unsuspended. Detach fallback from the DOM, then shift children into it.
(false, Some(_)) => {
match suspense.fallback.take() {
Some(Fallback::Bundle(bundle)) => {
bundle.detach(root, parent, false);
}
#[cfg(feature = "hydration")]
Some(Fallback::Fragment(fragment)) => {
fragment.detach(root, parent, false);
}
None => {
unreachable!("None condition has been checked before.")
}
};
children_bundle.shift(parent, next_sibling.clone());
children.reconcile_node(root, parent_scope, parent, next_sibling, children_bundle)
}
}
}
}
#[cfg(feature = "hydration")]
mod feat_hydration {
use super::*;
use crate::dom_bundle::{Fragment, Hydratable};
use crate::virtual_dom::Collectable;
impl Hydratable for VSuspense {
fn hydrate(
self,
root: &BSubtree,
parent_scope: &AnyScope,
parent: &Element,
fragment: &mut Fragment,
) -> (NodeRef, Self::Bundle) {
let detached_parent = document()
.create_element("div")
.expect("failed to create detached element");
let collectable = Collectable::Suspense;
let fallback_fragment = Fragment::collect_between(fragment, &collectable, parent);
let mut nodes = fallback_fragment.deep_clone();
for node in nodes.iter() {
detached_parent.append_child(node).unwrap();
}
let (_, children_bundle) =
self.children
.hydrate(root, parent_scope, &detached_parent, &mut nodes);
// We trim all leading text nodes before checking as it's likely these are whitespaces.
nodes.trim_start_text_nodes(&detached_parent);
assert!(nodes.is_empty(), "expected end of suspense, found node.");
let node_ref = fallback_fragment
.front()
.cloned()
.map(NodeRef::new)
.unwrap_or_default();
(
node_ref,
BSuspense {
children_bundle,
detached_parent,
key: self.key,
// We start hydration with the BSuspense being suspended.
// A subsequent render will resume the BSuspense if not needed to be suspended.
fallback: Some(Fallback::Fragment(fallback_fragment)),
},
)
}
}
}