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 55 56 57 58 59
use super::prelude::*; pub struct Context<A: Actor> { pub(super) inbox: mpsc::UnboundedReceiver<Box<dyn Envelope<Actor = A>>>, pub(super) keep_running: bool, addr: Addr<A>, } impl<A: Actor> Context<A> { pub(super) fn new() -> Self { let (tx, rx) = mpsc::unbounded_channel(); Self { inbox: rx, addr: Addr::new(tx), keep_running: true, } } } impl<A: Actor> Context<A> { #[inline] pub fn addr(&self) -> Addr<A> { self.addr.clone() } #[inline] pub fn notify_after<M: Message<Response = ()>>( &self, duration: Duration, msg_factory: impl FnOnce() -> M + Send + 'static, ) where A: Receive<M>, { let addr = self.addr(); tokio::spawn(async move { delay_for(duration).await; let _ = addr.tell(msg_factory()); }); } #[inline] pub fn notify_periodic<M: Message<Response = ()>>( &self, duration: Duration, msg_factory: impl FnOnce() -> M + Clone + Send + 'static, ) where A: Receive<M>, { let addr = self.addr(); tokio::spawn(async move { loop { delay_for(duration).await; let _ = addr.tell((msg_factory.clone())()); } }); } #[inline] pub fn stop(&mut self) { self.keep_running = false; } }