use crate::concurrency::Duration;
use crate::concurrency::JoinHandle;
use crate::ActorCell;
use crate::Message;
use crate::MessagingErr;
use crate::ACTIVE_STATES;
#[cfg(test)]
mod tests;
pub fn send_interval<TMessage, F>(period: Duration, actor: ActorCell, msg: F) -> JoinHandle<()>
where
TMessage: Message,
F: Fn() -> TMessage + Send + 'static,
{
crate::concurrency::spawn(async move {
let mut timer = crate::concurrency::interval(period);
timer.tick().await;
while ACTIVE_STATES.contains(&actor.get_status()) {
timer.tick().await;
if actor.send_message::<TMessage>(msg()).is_err() {
break;
}
}
})
}
pub fn send_after<TMessage, F>(
period: Duration,
actor: ActorCell,
msg: F,
) -> JoinHandle<Result<(), MessagingErr<TMessage>>>
where
TMessage: Message,
F: FnOnce() -> TMessage + Send + 'static,
{
crate::concurrency::spawn(async move {
crate::concurrency::sleep(period).await;
actor.send_message::<TMessage>(msg())
})
}
pub fn exit_after(period: Duration, actor: ActorCell) -> JoinHandle<()> {
crate::concurrency::spawn(async move {
crate::concurrency::sleep(period).await;
actor.stop(Some(format!("Exit after {}ms", period.as_millis())))
})
}
pub fn kill_after(period: Duration, actor: ActorCell) -> JoinHandle<()> {
crate::concurrency::spawn(async move {
crate::concurrency::sleep(period).await;
actor.kill()
})
}
impl<TMessage> crate::ActorRef<TMessage>
where
TMessage: crate::Message,
{
pub fn send_interval<F>(&self, period: Duration, msg: F) -> JoinHandle<()>
where
F: Fn() -> TMessage + Send + 'static,
{
send_interval::<TMessage, F>(period, self.get_cell(), msg)
}
pub fn send_after<F>(
&self,
period: Duration,
msg: F,
) -> JoinHandle<Result<(), MessagingErr<TMessage>>>
where
F: FnOnce() -> TMessage + Send + 'static,
{
send_after::<TMessage, F>(period, self.get_cell(), msg)
}
pub fn exit_after(&self, period: Duration) -> JoinHandle<()> {
exit_after(period, self.get_cell())
}
pub fn kill_after(&self, period: Duration) -> JoinHandle<()> {
kill_after(period, self.get_cell())
}
}
impl<TMessage> crate::DerivedActorRef<TMessage>
where
TMessage: crate::Message,
{
pub fn send_interval<F>(&self, period: Duration, msg: F) -> JoinHandle<()>
where
F: Fn() -> TMessage + Send + 'static,
{
let self_clone = self.clone();
crate::concurrency::spawn(async move {
let mut timer = crate::concurrency::interval(period);
timer.tick().await;
while ACTIVE_STATES.contains(&self_clone.get_status()) {
timer.tick().await;
if self_clone.send_message(msg()).is_err() {
break;
}
}
})
}
pub fn send_after<F>(
&self,
period: Duration,
msg: F,
) -> JoinHandle<Result<(), MessagingErr<TMessage>>>
where
F: FnOnce() -> TMessage + Send + 'static,
{
let self_clone = self.clone();
crate::concurrency::spawn(async move {
crate::concurrency::sleep(period).await;
let msg = msg();
self_clone.send_message(msg)
})
}
pub fn exit_after(&self, period: Duration) -> JoinHandle<()> {
exit_after(period, self.get_cell())
}
pub fn kill_after(&self, period: Duration) -> JoinHandle<()> {
kill_after(period, self.get_cell())
}
}