use std::time::{Duration, Instant};
use futures::StreamExt;
use messages::prelude::*;
use tokio_stream::wrappers::IntervalStream;
#[derive(Debug)]
pub struct Service {
last_notified: Instant,
}
impl Actor for Service {}
#[derive(Debug)]
pub struct Notification;
#[async_trait]
impl Notifiable<Notification> for Service {
async fn notify(&mut self, _input: Notification, _: &Context<Self>) {
println!(
"Notified after {}ms",
self.last_notified.elapsed().as_millis()
);
self.last_notified = Instant::now();
}
}
impl Service {
pub fn create() -> Self {
Self {
last_notified: Instant::now(),
}
}
}
#[tokio::main]
async fn main() {
let address = Service::create().spawn();
let interval_stream = IntervalStream::new(tokio::time::interval(Duration::from_millis(100)))
.take(10)
.map(|_| Notification);
let join_handle = address.spawn_stream_forwarder(interval_stream);
join_handle.await.unwrap().unwrap();
}