-
Notifications
You must be signed in to change notification settings - Fork 1
/
05_timed_stream.rs
54 lines (43 loc) · 1.32 KB
/
05_timed_stream.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
//! This example shows how to notify actor at a certain interval.
//! It also demonstrates how to attach stream to an actor via its address.
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() {
// Start a service.
let address = Service::create().spawn();
// Attach a stream that will ping the service every 100ms.
// It will emit 10 values only.
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);
// Wait until stream yielded all its values.
join_handle.await.unwrap().unwrap();
}