/
lib.rs
75 lines (65 loc) · 2.27 KB
/
lib.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
#![recursion_limit = "256"]
use yew::{html, Component, ComponentLink, Html, ShouldRender};
pub struct Model {
link: ComponentLink<Self>,
opposite: Option<ComponentLink<Model>>,
selector: &'static str,
title: String,
}
pub enum Msg {
SetOpposite(ComponentLink<Model>),
SendToOpposite(String),
SetTitle(String),
}
impl Component for Model {
type Message = Msg;
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
Model {
link,
opposite: None,
selector: "",
title: "Nothing".into(),
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::SetOpposite(opposite) => {
self.opposite = Some(opposite);
}
Msg::SendToOpposite(title) => {
self.opposite
.as_mut()
.unwrap()
.send_message(Msg::SetTitle(title));
}
Msg::SetTitle(title) => {
let send_msg = match title.as_ref() {
"Ping" => Some(Msg::SetTitle("Pong".into())),
"Pong" => Some(Msg::SetTitle("Pong Done".into())),
"Pong Done" => Some(Msg::SetTitle("Ping Done".into())),
_ => None,
};
if let Some(send_msg) = send_msg {
self.opposite.as_mut().unwrap().send_message(send_msg);
}
self.title = title;
}
}
true
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {
<div>
<h3>{ format!("{} received <{}>", self.selector, self.title) }</h3>
<button onclick=self.link.callback(|_| Msg::SendToOpposite("One".into()))>{ "One" }</button>
<button onclick=self.link.callback(|_| Msg::SendToOpposite("Two".into()))>{ "Two" }</button>
<button onclick=self.link.callback(|_| Msg::SendToOpposite("Three".into()))>{ "Three" }</button>
<button onclick=self.link.callback(|_| Msg::SendToOpposite("Ping".into()))>{ "Ping" }</button>
</div>
}
}
}