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
use crate::{
diff,
dom::{
apply_patches::patch,
created_node::{ActiveClosure, CreatedNode},
Dispatch,
},
};
use wasm_bindgen::JsCast;
use web_sys::{self, Element, Node};
pub struct DomUpdater<MSG> {
current_vdom: crate::Node<MSG>,
root_node: Node,
pub active_closures: ActiveClosure,
}
impl<MSG> DomUpdater<MSG> {
pub fn new(
current_vdom: crate::Node<MSG>,
mount: &Node,
) -> DomUpdater<MSG> {
DomUpdater {
current_vdom,
root_node: mount.clone(),
active_closures: ActiveClosure::new(),
}
}
pub fn active_closure_len(&self) -> usize {
self.active_closures
.iter()
.map(|(_elm_id, closures)| closures.len())
.sum()
}
}
impl<MSG> DomUpdater<MSG>
where
MSG: 'static,
{
pub fn append_to_mount<DSP>(&mut self, program: &DSP)
where
DSP: Dispatch<MSG> + Clone + 'static,
{
self.mount(program, false);
}
fn mount<DSP>(&mut self, program: &DSP, replace: bool)
where
DSP: Dispatch<MSG> + Clone + 'static,
{
let created_node: CreatedNode<Node> =
CreatedNode::<Node>::create_dom_node(program, &self.current_vdom);
if replace {
let root_element: &Element = self.root_node.unchecked_ref();
root_element
.replace_with_with_node_1(&created_node.node)
.expect("Could not append child to mount");
} else {
self.root_node
.append_child(&created_node.node)
.expect("Could not append child to mount");
}
self.root_node = created_node.node;
self.active_closures = created_node.closures;
}
pub fn replace_mount<DSP>(&mut self, program: &DSP)
where
DSP: Dispatch<MSG> + Clone + 'static,
{
self.mount(program, true);
}
pub fn new_append_to_mount<DSP>(
program: &DSP,
current_vdom: crate::Node<MSG>,
mount: &Element,
) -> DomUpdater<MSG>
where
DSP: Dispatch<MSG> + Clone + 'static,
{
let mut dom_updater = Self::new(current_vdom, mount);
dom_updater.append_to_mount(program);
dom_updater
}
pub fn new_replace_mount<DSP>(
program: &DSP,
current_vdom: crate::Node<MSG>,
mount: Element,
) -> DomUpdater<MSG>
where
DSP: Dispatch<MSG> + Clone + 'static,
{
let mut dom_updater = Self::new(current_vdom, &mount);
dom_updater.replace_mount(program);
dom_updater
}
pub fn update_dom<DSP>(&mut self, program: &DSP, new_vdom: crate::Node<MSG>)
where
DSP: Dispatch<MSG> + Clone + 'static,
{
let patches = diff(&self.current_vdom, &new_vdom);
let active_closures = patch(
Some(program),
self.root_node.clone(),
&mut self.active_closures,
patches,
)
.expect("Error in patching the dom");
self.active_closures.extend(active_closures);
self.current_vdom = new_vdom;
}
pub fn map_msg<F, MSG2>(self, func: F) -> DomUpdater<MSG2>
where
F: Fn(MSG) -> MSG2 + 'static,
MSG2: 'static,
{
let DomUpdater {
current_vdom,
root_node,
active_closures,
} = self;
DomUpdater {
current_vdom: current_vdom.map_msg(func),
root_node,
active_closures,
}
}
pub fn root_node(&self) -> Node {
self.root_node.clone()
}
}