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
use crate::{
dom::dom_updater::DomUpdater,
Cmd,
Component,
Dispatch,
};
use std::{
cell::RefCell,
rc::Rc,
};
#[cfg(feature = "with-request-animation-frame")]
use wasm_bindgen::closure::Closure;
use web_sys::Node;
pub struct Program<APP, MSG>
where
MSG: 'static,
{
pub app: Rc<RefCell<APP>>,
pub dom_updater: Rc<RefCell<DomUpdater<MSG>>>,
}
impl<APP, MSG> Clone for Program<APP, MSG>
where
MSG: 'static,
{
fn clone(&self) -> Self {
Program {
app: Rc::clone(&self.app),
dom_updater: Rc::clone(&self.dom_updater),
}
}
}
impl<APP, MSG> Program<APP, MSG>
where
MSG: 'static,
APP: Component<MSG> + 'static,
{
fn new(app: APP, root_node: &Node) -> Self {
let dom_updater: DomUpdater<MSG> =
DomUpdater::new(app.view(), root_node);
let program = Program {
app: Rc::new(RefCell::new(app)),
dom_updater: Rc::new(RefCell::new(dom_updater)),
};
program.init_emit();
program
}
fn init_emit(&self) {
let cmds: Cmd<APP, MSG> = self.app.borrow().init();
cmds.emit(&self);
}
fn after_mounted(&self) {
for style in self.app.borrow().style() {
Self::inject_style(&style);
}
}
#[allow(unused)]
fn root_node(&self) -> web_sys::Node {
self.dom_updater.borrow().root_node()
}
pub fn new_replace_mount(app: APP, root_node: &Node) -> Self {
let program = Self::new(app, root_node);
program.start_replace_mount();
program.after_mounted();
program
}
pub fn new_append_to_mount(app: APP, root_node: &Node) -> Self {
let program = Self::new(app, root_node);
program.start_append_to_mount();
program.after_mounted();
program
}
pub fn mount_to_body(app: APP) -> Self {
Self::new_append_to_mount(app, &crate::body())
}
fn start_append_to_mount(&self) {
self.dom_updater.borrow_mut().append_to_mount(self)
}
fn start_replace_mount(&self) {
self.dom_updater.borrow_mut().replace_mount(self)
}
fn dispatch_inner(&self, msg: MSG) {
#[cfg(feature = "with-measure")]
let t1 = crate::now();
let cmd = self.app.borrow_mut().update(msg);
cmd.emit(self);
#[cfg(feature = "with-measure")]
let t2 = {
let t2 = crate::now();
log::trace!("app update took: {}ms", t2 - t1);
t2
};
let view = self.app.borrow().view();
#[cfg(feature = "with-measure")]
let t3 = {
let t3 = crate::now();
log::trace!("creating app view took: {}ms", t3 - t2);
t3
};
self.dom_updater.borrow_mut().update_dom(self, view);
#[cfg(feature = "with-measure")]
{
let t4 = crate::now();
log::trace!("dom update took: {}ms", t4 - t3);
let dispatch_duration = t4 - t1;
if dispatch_duration > 16.0 {
log::warn!("dispatch took: {}ms", dispatch_duration);
} else {
log::trace!("dispatch took: {}ms", dispatch_duration);
}
};
}
fn inject_style(style: &str) {
use wasm_bindgen::JsCast;
let document = crate::document();
let html_style = document
.create_element("style")
.expect("must be able to create style element");
html_style
.set_attribute("name", "style_name")
.expect("must set attribute");
let html_style: web_sys::Node = html_style.unchecked_into();
html_style.set_text_content(Some(style));
let head = document.head().expect("must have a head");
head.append_child(&html_style).expect("must append style");
}
}
impl<APP, MSG> Dispatch<MSG> for Program<APP, MSG>
where
MSG: 'static,
APP: Component<MSG> + 'static,
{
#[cfg(feature = "with-request-animation-frame")]
fn dispatch(&self, msg: MSG) {
let program_clone = self.clone();
let closure_raf: Closure<dyn FnMut() + 'static> =
Closure::once(move || {
program_clone.dispatch_inner(msg);
});
crate::request_animation_frame(&closure_raf);
closure_raf.forget();
}
#[cfg(not(feature = "with-request-animation-frame"))]
fn dispatch(&self, msg: MSG) {
self.dispatch_inner(msg)
}
}