-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmain.rs
158 lines (139 loc) · 4.42 KB
/
main.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
use gloo::console::{self, Timer};
use gloo::timers::callback::{Interval, Timeout};
use yew::prelude::*;
pub enum Msg {
StartTimeout,
StartInterval,
Cancel,
Done,
Tick,
UpdateTime,
}
pub struct App {
time: String,
messages: Vec<AttrValue>,
_standalone: (Interval, Interval),
interval: Option<Interval>,
timeout: Option<Timeout>,
console_timer: Option<Timer<'static>>,
}
impl App {
fn get_current_time() -> String {
let date = js_sys::Date::new_0();
String::from(date.to_locale_time_string("en-US"))
}
fn cancel(&mut self) {
self.timeout = None;
self.interval = None;
}
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
let standalone_handle =
Interval::new(10, || console::debug!("Example of a standalone callback."));
let clock_handle = {
let link = ctx.link().clone();
Interval::new(1, move || link.send_message(Msg::UpdateTime))
};
Self {
time: App::get_current_time(),
messages: Vec::new(),
_standalone: (standalone_handle, clock_handle),
interval: None,
timeout: None,
console_timer: None,
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::StartTimeout => {
let handle = {
let link = ctx.link().clone();
Timeout::new(3000, move || link.send_message(Msg::Done))
};
self.timeout = Some(handle);
self.messages.clear();
console::clear!();
self.log("Timer started!");
self.console_timer = Some(Timer::new("Timer"));
true
}
Msg::StartInterval => {
let handle = {
let link = ctx.link().clone();
Interval::new(1000, move || link.send_message(Msg::Tick))
};
self.interval = Some(handle);
self.messages.clear();
console::clear!();
self.log("Interval started!");
true
}
Msg::Cancel => {
self.cancel();
self.log("Canceled!");
console::warn!("Canceled!");
true
}
Msg::Done => {
self.cancel();
self.log("Done!");
// todo weblog
// ConsoleService::group();
console::info!("Done!");
if let Some(timer) = self.console_timer.take() {
drop(timer);
}
// todo weblog
// ConsoleService::group_end();
true
}
Msg::Tick => {
self.log("Tick...");
// todo weblog
// ConsoleService::count_named("Tick");
true
}
Msg::UpdateTime => {
self.time = App::get_current_time();
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let has_job = self.timeout.is_some() || self.interval.is_some();
html! {
<>
<div id="buttons">
<button disabled={has_job} onclick={ctx.link().callback(|_| Msg::StartTimeout)}>
{ "Start Timeout" }
</button>
<button disabled={has_job} onclick={ctx.link().callback(|_| Msg::StartInterval)}>
{ "Start Interval" }
</button>
<button disabled={!has_job} onclick={ctx.link().callback(|_| Msg::Cancel)}>
{ "Cancel!" }
</button>
</div>
<div id="wrapper">
<div id="time">
{ &self.time }
</div>
<div id="messages">
{ for self.messages.iter().map(|message| html! { <p>{ message }</p> }) }
</div>
</div>
</>
}
}
}
impl App {
fn log(&mut self, message: impl Into<AttrValue>) {
self.messages.push(message.into());
}
}
fn main() {
yew::Renderer::<App>::new().render();
}