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
#![warn(
missing_docs,
trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
unused_results,
)]
#[macro_use]
extern crate log;
extern crate relm_core;
mod into;
mod macros;
use std::time::SystemTime;
pub use relm_core::EventStream;
pub use into::{IntoOption, IntoPair};
pub struct Relm<UPDATE: Update> {
stream: EventStream<UPDATE::Msg>,
}
impl<UPDATE: Update> Clone for Relm<UPDATE> {
fn clone(&self) -> Self {
Relm {
stream: self.stream.clone(),
}
}
}
impl<UPDATE: Update> Relm<UPDATE> {
pub fn new(stream: EventStream<UPDATE::Msg>) -> Self {
Relm {
stream,
}
}
pub fn stream(&self) -> &EventStream<UPDATE::Msg> {
&self.stream
}
}
pub trait Update
where Self: Sized,
Self::Msg: DisplayVariant,
{
type Model;
type ModelParam: Sized;
type Msg;
fn model(relm: &Relm<Self>, param: Self::ModelParam) -> Self::Model;
fn subscriptions(&mut self, _relm: &Relm<Self>) {
}
fn update(&mut self, event: Self::Msg);
}
pub trait UpdateNew: Update {
fn new(_relm: &Relm<Self>, _model: Self::Model) -> Self;
}
pub trait DisplayVariant {
fn display_variant(&self) -> &'static str;
}
impl DisplayVariant for () {
fn display_variant(&self) -> &'static str {
""
}
}
pub fn execute<UPDATE>(model_param: UPDATE::ModelParam) -> EventStream<UPDATE::Msg>
where UPDATE: Update + UpdateNew + 'static
{
let stream = EventStream::new();
let relm = Relm::new(stream.clone());
let model = UPDATE::model(&relm, model_param);
let component = UPDATE::new(&relm, model);
init_component::<UPDATE>(&stream, component, &relm);
stream
}
pub fn init_component<UPDATE>(stream: &EventStream<UPDATE::Msg>, mut component: UPDATE, relm: &Relm<UPDATE>)
where UPDATE: Update + 'static,
UPDATE::Msg: DisplayVariant + 'static,
{
component.subscriptions(relm);
stream.set_callback(move |event| {
update_component(&mut component, event);
});
}
fn update_component<COMPONENT>(component: &mut COMPONENT, event: COMPONENT::Msg)
where COMPONENT: Update,
{
if cfg!(debug_assertions) {
let time = SystemTime::now();
let debug = event.display_variant();
let debug =
if debug.len() > 100 {
format!("{}…", &debug[..100])
}
else {
debug.to_string()
};
component.update(event);
if let Ok(duration) = time.elapsed() {
let ms = duration.subsec_nanos() as u64 / 1_000_000 + duration.as_secs() * 1000;
if ms >= 16 {
warn!("The update function was slow to execute for message {}: {}ms", debug, ms);
}
}
}
else {
component.update(event)
}
}