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
use crate::{dom::dom_updater::DomUpdater, Application, Dispatch};
use std::{cell::RefCell, rc::Rc};
#[cfg(feature = "with-request-animation-frame")]
use wasm_bindgen::closure::Closure;
use web_sys::Node;
use crate::dom::Measurements;
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: Application<MSG> + 'static,
{
pub 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
}
fn init_emit(&self) {
let cmds = self.app.borrow_mut().init(self.clone());
cmds.emit(&self);
}
fn after_mounted(&self) {
for style in self.app.borrow().style() {
Self::inject_style(&style);
}
self.init_emit();
}
#[allow(unused)]
fn root_node(&self) -> web_sys::Node {
self.dom_updater.borrow().root_node()
}
pub fn 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 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::append_to_mount(app, &crate::body())
}
pub fn replace_body(app: APP) -> Self {
Self::replace_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) {
let t1 = crate::now();
let cmd = self.app.borrow_mut().update(msg);
if cmd.should_update_view {
let t2 = crate::now();
#[cfg(feature = "with-measure")]
log::trace!("app update took: {}ms", t2 - t1);
let view = self.app.borrow().view();
let node_count = view.node_count();
let t3 = crate::now();
#[cfg(feature = "with-measure")]
log::trace!("creating app view took: {}ms", t3 - t2);
self.dom_updater.borrow_mut().update_dom(self, view);
let t4 = crate::now();
#[cfg(feature = "with-measure")]
log::trace!("dom update took: {}ms", t4 - t3);
#[cfg(feature = "with-measure")]
{
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);
}
}
if cmd.log_measurements {
let measurements = Measurements {
view_node_count: node_count,
update_dispatch_took: t2 - t1,
build_view_took: t3 - t2,
dom_update_took: t4 - t3,
total_time: t4 - t1,
};
let mut cmd_measurement =
self.app.borrow().measurements(measurements);
cmd_measurement.should_update_view = false;
cmd_measurement.emit(self);
} else {
#[cfg(any(feature = "with-debug", feature = "with-measure"))]
log::info!("skipped logging measurements");
}
} else {
#[cfg(any(feature = "with-debug", feature = "with-measure"))]
log::info!("dom update is skipped here");
}
cmd.emit(self);
}
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: Application<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)
}
}