-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
bsuspense.rs
178 lines (163 loc) · 6 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
//! This module contains the bundle version of a supsense [BSuspense]
use super::{BNode, DomBundle, Reconcilable};
use crate::html::AnyScope;
use crate::virtual_dom::{Key, VSuspense};
use crate::NodeRef;
use web_sys::Element;
/// The bundle implementation to [VSuspense]
#[derive(Debug)]
pub struct BSuspense {
children_bundle: BNode,
/// The supsense is suspended if fallback contains [Some] bundle
fallback_bundle: Option<BNode>,
detached_parent: Element,
key: Option<Key>,
}
impl BSuspense {
/// Get the key of the underlying suspense
pub(super) fn key(&self) -> Option<&Key> {
self.key.as_ref()
}
/// Get the bundle node that actually shows up in the dom
fn active_node(&self) -> &BNode {
self.fallback_bundle
.as_ref()
.unwrap_or(&self.children_bundle)
}
}
impl DomBundle for BSuspense {
fn detach(self, parent: &Element, parent_to_detach: bool) {
if let Some(fallback) = self.fallback_bundle {
fallback.detach(parent, parent_to_detach);
self.children_bundle.detach(&self.detached_parent, false);
} else {
self.children_bundle.detach(parent, parent_to_detach);
}
}
fn shift(&self, next_parent: &Element, next_sibling: NodeRef) {
self.active_node().shift(next_parent, next_sibling)
}
}
impl Reconcilable for VSuspense {
type Bundle = BSuspense;
fn attach(
self,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
) -> (NodeRef, Self::Bundle) {
let VSuspense {
children,
fallback,
detached_parent,
suspended,
key,
} = self;
let detached_parent = detached_parent.expect("no 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.
if suspended {
let (_child_ref, children_bundle) =
children.attach(parent_scope, &detached_parent, NodeRef::default());
let (fallback_ref, fallback) = fallback.attach(parent_scope, parent, next_sibling);
(
fallback_ref,
BSuspense {
children_bundle,
fallback_bundle: Some(fallback),
detached_parent,
key,
},
)
} else {
let (child_ref, children_bundle) = children.attach(parent_scope, parent, next_sibling);
(
child_ref,
BSuspense {
children_bundle,
fallback_bundle: None,
detached_parent,
key,
},
)
}
}
fn reconcile_node(
self,
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.detached_parent.as_ref() == Some(&m.detached_parent) =>
{
self.reconcile(parent_scope, parent, next_sibling, m)
}
_ => self.replace(parent_scope, parent, next_sibling, bundle),
}
}
fn reconcile(
self,
parent_scope: &AnyScope,
parent: &Element,
next_sibling: NodeRef,
suspense: &mut Self::Bundle,
) -> NodeRef {
let VSuspense {
children,
fallback,
detached_parent,
suspended,
key: _,
} = self;
let detached_parent = detached_parent.expect("no detached parent?");
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_bundle) {
// Both suspended, reconcile children into detached_parent, fallback into the DOM
(true, Some(fallback_bundle)) => {
children.reconcile_node(
parent_scope,
&detached_parent,
NodeRef::default(),
children_bundle,
);
fallback.reconcile_node(parent_scope, parent, next_sibling, fallback_bundle)
}
// Not suspended, just reconcile the children into the DOM
(false, None) => {
children.reconcile_node(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(&detached_parent, NodeRef::default());
children.reconcile_node(
parent_scope,
&detached_parent,
NodeRef::default(),
children_bundle,
);
// first render of fallback
let (fallback_ref, fallback) = fallback.attach(parent_scope, parent, next_sibling);
suspense.fallback_bundle = Some(fallback);
fallback_ref
}
// Freshly unsuspended. Detach fallback from the DOM, then shift children into it.
(false, Some(_)) => {
suspense
.fallback_bundle
.take()
.unwrap() // We just matched Some(_)
.detach(parent, false);
children_bundle.shift(parent, next_sibling.clone());
children.reconcile_node(parent_scope, parent, next_sibling, children_bundle)
}
}
}
}