a virtual view in rust
```rust extern crate messenger; extern crate serde_json; extern crate tokio;
extern crate virtual_view;
use messenger::unboundedchannel; use tokio::executor::currentthread; use virtual_view::{Children, Component, EventManager, Instance, Prop, Props, Renderer, Updater, View};
struct Button;
impl Component for Button { fn name(&self) -> &'static str { "Button" } fn render(&self, _: &Instance, props: &Props, children: &Children) -> View { view! {
struct Counter;
impl Counter { fn onaddcount(updater: &Updater) -> Prop { updater.set_state(|current| { let mut next = current.clone();
next.update("count", |count| {
if let Some(c) = count.number() {
*count = (c + 1.0).into();
}
});
next
});
Prop::Null
}
fn on_sub_count(updater: &Updater) -> Prop {
updater.set_state(|current| {
let mut next = current.clone();
next.update("count", |count| {
if let Some(c) = count.number() {
*count = (c - 1.0).into();
}
});
next
});
Prop::Null
}
}
impl Component for Counter {
fn name(&self) -> &'static str {
"Counter"
}
fn initialstate(&self, props: &Props) -> Props {
props! {
"count": props.take("count").unwrapor(0.into())
}
}
fn render(&self, instance: &Instance, : &Props, _: &Children) -> View {
view! {
{format!("Count {}", instance.state.get("count"))}
fn main() { let (server, client, future) = unbounded_channel();
let event_manager = EventManager::new();
let _renderer = Renderer::new(
view! {
<{Counter} count=0/>
},
event_manager.clone(),
server,
);
let c = client.clone();
let _ = client.on("virtual_view.transaction", move |t| {
println!("{}", t);
c.close();
None
});
current_thread::run(move |_| {
let _ = current_thread::spawn(future);
});
} ```