/
main.rs
84 lines (73 loc) · 2.63 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
use yew::html::Scope;
use yew::{html, AppHandle, Component, Context, Html};
pub enum Msg {
SetOpposite(Scope<App>),
SendToOpposite(String),
SetTitle(String),
}
pub struct App {
opposite: Option<Scope<App>>,
selector: &'static str,
title: String,
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
App {
opposite: None,
selector: "",
title: "Nothing".to_owned(),
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::SetOpposite(opposite) => {
self.opposite = Some(opposite);
false
}
Msg::SendToOpposite(title) => {
self.opposite
.as_mut()
.unwrap()
.send_message(Msg::SetTitle(title));
false
}
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 view(&self, ctx: &Context<Self>) -> Html {
html! {
<div>
<h3>{ format!("{} received <{}>", self.selector, self.title) }</h3>
<button onclick={ctx.link().callback(|_| Msg::SendToOpposite("One".into()))}>{ "One" }</button>
<button onclick={ctx.link().callback(|_| Msg::SendToOpposite("Two".into()))}>{ "Two" }</button>
<button onclick={ctx.link().callback(|_| Msg::SendToOpposite("Three".into()))}>{ "Three" }</button>
<button onclick={ctx.link().callback(|_| Msg::SendToOpposite("Ping".into()))}>{ "Ping" }</button>
</div>
}
}
}
fn mount_app(selector: &'static str) -> AppHandle<App> {
let document = gloo::utils::document();
let element = document.query_selector(selector).unwrap().unwrap();
yew::Renderer::<App>::with_root(element).render()
}
fn main() {
let first_app = mount_app(".first-app");
let second_app = mount_app(".second-app");
first_app.send_message(Msg::SetOpposite(second_app.clone()));
second_app.send_message(Msg::SetOpposite(first_app.clone()));
}