use std::time::Duration;
use act_zero::runtimes::tokio::{spawn_actor, Timer};
use act_zero::timer::Tick;
use act_zero::*;
use async_trait::async_trait;
use tokio::task::spawn_blocking;
#[derive(Default)]
struct LonelyActor {
addr: WeakAddr<Self>,
timer: Timer,
}
#[async_trait]
impl Actor for LonelyActor {
async fn started(&mut self, addr: Addr<Self>) -> ActorResult<()> {
self.addr = addr.downgrade();
self.comfort().await;
Produces::ok(())
}
}
impl LonelyActor {
async fn comfort(&mut self) {
println!(":)");
self.timer
.set_timeout_for_weak(self.addr.clone(), Duration::from_secs(10));
}
}
#[async_trait]
impl Tick for LonelyActor {
async fn tick(&mut self) -> ActorResult<()> {
if self.timer.tick() {
println!("Are you still there?");
}
Produces::ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), ActorError> {
let addr = spawn_actor(LonelyActor::default());
spawn_blocking(move || {
let mut input = String::new();
loop {
std::io::stdin().read_line(&mut input)?;
send!(addr.comfort());
}
})
.await?
}