use lakka::*;
use std::time::Duration;
#[derive(Default)]
struct Counter {
counter: i32,
}
#[messages]
impl Counter {
fn inc(&mut self) {
self.counter += 1;
}
fn get(&self) -> i32 {
self.counter
}
}
#[tokio::main]
async fn main() {
let counter_handle = Counter::default().run();
_ = counter_handle.inc().await;
_ = counter_handle.inc().await;
let state = counter_handle.get().await.unwrap();
assert_eq!(state, 2);
let second_handle = counter_handle.clone();
tokio::spawn(async move {
loop {
second_handle.inc().await.unwrap();
tokio::time::sleep(Duration::from_millis(20)).await;
}
});
tokio::time::sleep(Duration::from_secs(1)).await;
println!("Counter value: {}", counter_handle.get().await.unwrap());
}