-
Notifications
You must be signed in to change notification settings - Fork 69
/
time_test.rs
275 lines (232 loc) · 8.02 KB
/
time_test.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
#[cfg(feature = "chrono")]
mod shared {
use chrono::{DateTime, Utc};
use crux_core::macros::Effect;
use crux_core::render::Render;
use crux_time::{Time, TimeResponse, TimerId};
use serde::{Deserialize, Serialize};
#[derive(Default)]
pub struct App;
#[derive(Serialize, Deserialize)]
pub enum Event {
Get,
GetAsync,
Set(TimeResponse),
StartDebounce,
DurationElapsed(usize, TimeResponse),
}
#[derive(Default)]
struct Debounce {
pending: usize,
}
impl Debounce {
fn start(&mut self) -> usize {
self.pending = self.pending.wrapping_add(1);
self.pending
}
fn resolve(&mut self, pending: usize) -> bool {
self.pending == pending
}
}
#[derive(Default)]
pub struct Model {
pub time: String,
debounce: Debounce,
pub debounce_complete: bool,
pub debounce_time_id: Option<TimerId>,
}
#[derive(Serialize, Deserialize, Default)]
pub struct ViewModel {
pub time: String,
}
impl crux_core::App for App {
type Event = Event;
type Model = Model;
type ViewModel = ViewModel;
type Capabilities = Capabilities;
fn update(&self, event: Event, model: &mut Model, caps: &Capabilities) {
match event {
Event::Get => caps.time.now(Event::Set),
Event::GetAsync => caps.compose.spawn(|ctx| {
let time = caps.time.clone();
async move {
ctx.update_app(Event::Set(time.now_async().await));
}
}),
Event::Set(time) => {
if let TimeResponse::Now(time) = time {
let time: DateTime<Utc> = time.try_into().unwrap();
model.time = time.to_rfc3339();
caps.render.render()
}
}
Event::StartDebounce => {
let pending = model.debounce.start();
let tid = caps.time.notify_after(
crux_time::Duration::from_millis(300).expect("valid duration"),
event_with_user_info(pending, Event::DurationElapsed),
);
model.debounce_time_id = Some(tid);
}
Event::DurationElapsed(pending, TimeResponse::DurationElapsed { id: _ }) => {
if model.debounce.resolve(pending) {
model.debounce_complete = true;
}
}
Event::DurationElapsed(_, TimeResponse::Cleared { id }) => {
if let Some(tid) = model.debounce_time_id {
if tid == id {
model.debounce_time_id = None;
}
}
}
Event::DurationElapsed(_, _) => {
panic!("Unexpected debounce event")
}
}
}
fn view(&self, model: &Self::Model) -> Self::ViewModel {
ViewModel {
time: model.time.clone(),
}
}
}
#[derive(Effect)]
pub struct Capabilities {
pub time: Time<Event>,
pub render: Render<Event>,
#[effect(skip)]
pub compose: crux_core::compose::Compose<Event>,
}
/// Helper to create an event with additional user info captured
/// this is effectively partially applying the event constructor
pub fn event_with_user_info<E, F, U, T>(user_info: U, make_event: F) -> impl FnOnce(T) -> E
where
F: FnOnce(U, T) -> E,
{
move |response| make_event(user_info, response)
}
}
#[cfg(feature = "chrono")]
mod shell {
use super::shared::{App, Effect, Event};
use chrono::{DateTime, Utc};
use crux_core::{Core, Request};
use crux_time::{Instant, TimeRequest, TimeResponse};
use std::collections::VecDeque;
pub enum Outcome {
Time(Request<TimeRequest>, Instant),
}
enum CoreMessage {
Event(Event),
Response(Outcome),
}
pub fn run(core: &Core<Effect, App>) {
let mut queue: VecDeque<CoreMessage> = VecDeque::new();
queue.push_back(CoreMessage::Event(Event::Get));
while !queue.is_empty() {
let msg = queue.pop_front();
let effs = match msg {
Some(CoreMessage::Event(m)) => core.process_event(m),
Some(CoreMessage::Response(Outcome::Time(mut request, result))) => {
core.resolve(&mut request, TimeResponse::Now(result))
}
_ => vec![],
};
for effect in effs {
if let Effect::Time(request) = effect {
let time: DateTime<Utc> =
"2022-12-01T01:47:12.746202562+00:00".parse().unwrap();
queue.push_back(CoreMessage::Response(Outcome::Time(
request,
time.try_into().unwrap(),
)));
}
}
}
}
}
#[cfg(feature = "chrono")]
mod tests {
use crate::{
shared::{App, Effect, Event, Model},
shell::run,
};
use chrono::{DateTime, Utc};
use crux_core::{testing::AppTester, Core};
use crux_time::TimeResponse;
#[test]
pub fn test_time() {
let core: Core<Effect, App> = Core::default();
run(&core);
assert_eq!(core.view().time, "2022-12-01T01:47:12.746202562+00:00");
}
#[test]
pub fn test_time_async() {
let app = AppTester::<App, _>::default();
let mut model = Model::default();
let request = &mut app
.update(Event::GetAsync, &mut model)
.expect_one_effect()
.expect_time();
let now: DateTime<Utc> = "2022-12-01T01:47:12.746202562+00:00".parse().unwrap();
let response = TimeResponse::Now(now.try_into().unwrap());
let _update = app.resolve_to_event_then_update(request, response, &mut model);
assert_eq!(app.view(&model).time, "2022-12-01T01:47:12.746202562+00:00");
}
#[test]
pub fn test_debounce_timer() {
let app = AppTester::<App, _>::default();
let mut model = Model::default();
let request1 = &mut app
.update(Event::StartDebounce, &mut model)
.expect_one_effect()
.expect_time();
let request2 = &mut app
.update(Event::StartDebounce, &mut model)
.expect_one_effect()
.expect_time();
// resolve and update
app.resolve_to_event_then_update(
request1,
TimeResponse::DurationElapsed {
id: model.debounce_time_id.unwrap(),
},
&mut model,
)
.assert_empty();
// resolving the first debounce should not set the debounce_complete flag
assert!(!model.debounce_complete);
// resolve and update
app.resolve_to_event_then_update(
request2,
TimeResponse::DurationElapsed {
id: model.debounce_time_id.unwrap(),
},
&mut model,
)
.assert_empty();
// resolving the second debounce should set the debounce_complete flag
assert!(model.debounce_complete);
}
#[test]
pub fn test_cancel_timer() {
let app = AppTester::<App, _>::default();
let mut model = Model::default();
let request1 = &mut app
.update(Event::StartDebounce, &mut model)
.expect_one_effect()
.expect_time();
assert!(model.debounce_time_id.is_some());
app.resolve_to_event_then_update(
request1,
TimeResponse::Cleared {
id: model.debounce_time_id.unwrap(),
},
&mut model,
)
.assert_empty();
assert!(!model.debounce_complete);
assert!(model.debounce_time_id.is_none());
}
}