forked from yewstack/yew
/
vcomp.rs
393 lines (344 loc) · 10.9 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! This module contains the implementation of a virtual component `VComp`.
use super::{Transformer, VDiff, VNode};
use crate::html::{Component, ComponentUpdate, HiddenScope, NodeRef, Scope};
use crate::utils::document;
use cfg_if::cfg_if;
use std::any::TypeId;
use std::fmt;
use std::mem::swap;
use std::rc::Rc;
cfg_if! {
if #[cfg(feature = "std_web")] {
use stdweb::web::{Element, INode, Node, TextNode};
} else if #[cfg(feature = "web_sys")] {
use web_sys::{Element, Node, Text as TextNode};
}
}
/// The method generates an instance of a component.
type Generator = dyn Fn(GeneratorType) -> Mounted;
/// Components can be generated by mounting or by overwriting an old component.
enum GeneratorType {
Mount(Element, TextNode),
Overwrite(HiddenScope),
}
/// A virtual component.
#[derive(Clone)]
pub struct VComp {
type_id: TypeId,
state: MountState,
pub(crate) node_ref: NodeRef,
pub(crate) key: Option<String>,
}
/// A virtual child component.
pub struct VChild<COMP: Component> {
/// The component properties
pub props: COMP::Properties,
/// Reference to the mounted node
node_ref: NodeRef,
key: Option<String>,
}
impl<COMP: Component> Clone for VChild<COMP> {
fn clone(&self) -> Self {
VChild {
props: self.props.clone(),
node_ref: self.node_ref.clone(),
key: self.key.clone(),
}
}
}
impl<COMP> VChild<COMP>
where
COMP: Component,
{
/// Creates a child component that can be accessed and modified by its parent.
pub fn new(props: COMP::Properties, node_ref: NodeRef, key: Option<String>) -> Self {
Self {
props,
node_ref,
key,
}
}
}
impl<COMP> From<VChild<COMP>> for VComp
where
COMP: Component,
{
fn from(vchild: VChild<COMP>) -> Self {
VComp::new::<COMP>(vchild.props, vchild.node_ref, vchild.key)
}
}
#[derive(Clone)]
enum MountState {
Unmounted(Unmounted),
Mounted(Mounted),
Mounting,
Detached,
Overwritten,
}
#[derive(Clone)]
struct Unmounted {
generator: Rc<Generator>,
}
struct Mounted {
node_ref: NodeRef,
scope: HiddenScope,
destroyer: Box<dyn FnOnce()>,
}
impl Clone for Mounted {
fn clone(&self) -> Self {
panic!("Mounted components are not allowed to be cloned!")
}
}
impl VComp {
/// This method prepares a generator to make a new instance of the `Component`.
pub fn new<COMP>(props: COMP::Properties, node_ref: NodeRef, key: Option<String>) -> Self
where
COMP: Component,
{
let node_ref_clone = node_ref.clone();
let generator = move |generator_type: GeneratorType| -> Mounted {
match generator_type {
GeneratorType::Mount(element, dummy_node) => {
let scope: Scope<COMP> = Scope::new();
let mut scope = scope.mount_in_place(
element,
Some(VNode::VRef(dummy_node.into())),
node_ref_clone.clone(),
props.clone(),
);
Mounted {
node_ref: node_ref_clone.clone(),
scope: scope.clone().into(),
destroyer: Box::new(move || scope.destroy()),
}
}
GeneratorType::Overwrite(hidden_scope) => {
let mut scope: Scope<COMP> = hidden_scope.into();
scope.update(ComponentUpdate::Properties(
props.clone(),
node_ref_clone.clone(),
));
Mounted {
node_ref: node_ref_clone.clone(),
scope: scope.clone().into(),
destroyer: Box::new(move || scope.destroy()),
}
}
}
};
VComp {
type_id: TypeId::of::<COMP>(),
state: MountState::Unmounted(Unmounted {
generator: Rc::new(generator),
}),
node_ref,
key,
}
}
}
impl Unmounted {
/// Mount a virtual component using a generator.
fn mount(self, parent: Element, dummy_node: TextNode) -> Mounted {
(self.generator)(GeneratorType::Mount(parent, dummy_node))
}
/// Overwrite an existing virtual component using a generator.
fn replace(self, old: Mounted) -> Mounted {
(self.generator)(GeneratorType::Overwrite(old.scope))
}
}
enum Reform {
Keep(Mounted),
Before(Option<Node>),
}
impl VDiff for VComp {
fn detach(&mut self, _parent: &Element) -> Option<Node> {
let mut replace_state = MountState::Detached;
swap(&mut replace_state, &mut self.state);
match replace_state {
MountState::Mounted(this) => {
(this.destroyer)();
this.node_ref.get().and_then(|node| node.next_sibling())
}
_ => None,
}
}
fn apply(
&mut self,
parent: &Element,
previous_sibling: Option<&Node>,
ancestor: Option<VNode>,
) -> Option<Node> {
let mut replace_state = MountState::Mounting;
swap(&mut replace_state, &mut self.state);
if let MountState::Unmounted(this) = replace_state {
let reform = match ancestor {
Some(VNode::VComp(mut vcomp)) => {
// If the ancestor is a Component of the same type, don't replace, keep the
// old Component but update the properties.
if self.type_id == vcomp.type_id {
let mut replace_state = MountState::Overwritten;
swap(&mut replace_state, &mut vcomp.state);
match replace_state {
MountState::Mounted(mounted) => Reform::Keep(mounted),
_ => Reform::Before(None),
}
} else {
Reform::Before(vcomp.detach(parent))
}
}
Some(mut vnode) => Reform::Before(vnode.detach(parent)),
None => Reform::Before(None),
};
let mounted = match reform {
Reform::Keep(mounted) => {
// Send properties update when the component is already rendered.
this.replace(mounted)
}
Reform::Before(next_sibling) => {
let dummy_node = document().create_text_node("");
if let Some(next_sibling) = next_sibling {
let next_sibling = &next_sibling;
#[cfg(feature = "web_sys")]
let next_sibling = Some(next_sibling);
parent
.insert_before(&dummy_node, next_sibling)
.expect("can't insert dummy component node before next sibling");
} else if let Some(next_sibling) =
previous_sibling.and_then(|p| p.next_sibling())
{
let next_sibling = &next_sibling;
#[cfg(feature = "web_sys")]
let next_sibling = Some(next_sibling);
parent
.insert_before(&dummy_node, next_sibling)
.expect("can't insert dummy component node before next sibling");
} else {
#[cfg_attr(
feature = "std_web",
allow(clippy::let_unit_value, unused_variables)
)]
{
let result = parent.append_child(&dummy_node);
#[cfg(feature = "web_sys")]
result.expect("can't append node to parent");
}
}
this.mount(parent.to_owned(), dummy_node)
}
};
self.state = MountState::Mounted(mounted);
}
None
}
}
impl<T> Transformer<T, T> for VComp {
fn transform(from: T) -> T {
from
}
}
impl<'a, T> Transformer<&'a T, T> for VComp
where
T: Clone,
{
fn transform(from: &'a T) -> T {
from.clone()
}
}
impl<'a> Transformer<&'a str, String> for VComp {
fn transform(from: &'a str) -> String {
from.to_owned()
}
}
impl<T> Transformer<T, Option<T>> for VComp {
fn transform(from: T) -> Option<T> {
Some(from)
}
}
impl<'a, T> Transformer<&'a T, Option<T>> for VComp
where
T: Clone,
{
fn transform(from: &T) -> Option<T> {
Some(from.clone())
}
}
impl<'a> Transformer<&'a str, Option<String>> for VComp {
fn transform(from: &'a str) -> Option<String> {
Some(from.to_owned())
}
}
impl<'a> Transformer<Option<&'a str>, Option<String>> for VComp {
fn transform(from: Option<&'a str>) -> Option<String> {
from.map(|s| s.to_owned())
}
}
impl PartialEq for VComp {
fn eq(&self, other: &VComp) -> bool {
self.type_id == other.type_id
}
}
impl fmt::Debug for VComp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VComp")
}
}
impl<COMP: Component> fmt::Debug for VChild<COMP> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("VChild<_>")
}
}
#[cfg(test)]
mod tests {
use crate::macros::Properties;
use crate::{html, Component, ComponentLink, Html, ShouldRender};
#[cfg(feature = "wasm_test")]
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
#[cfg(feature = "wasm_test")]
wasm_bindgen_test_configure!(run_in_browser);
struct Comp;
#[derive(Clone, PartialEq, Properties)]
struct Props {
#[prop_or_default]
field_1: u32,
#[prop_or_default]
field_2: u32,
}
impl Component for Comp {
type Message = ();
type Properties = Props;
fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
Comp
}
fn update(&mut self, _: Self::Message) -> ShouldRender {
unimplemented!();
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
unimplemented!();
}
fn view(&self) -> Html {
unimplemented!();
}
}
#[test]
fn set_properties_to_component() {
html! {
<Comp />
};
html! {
<Comp field_1=1 />
};
html! {
<Comp field_2=2 />
};
html! {
<Comp field_1=1 field_2=2 />
};
let props = Props {
field_1: 1,
field_2: 1,
};
html! {
<Comp with props />
};
}
}