use crate::actors::director::ActorsDirector;
use crate::services::handle::{Listen, Serve};
use crate::services::service::Service;
use crate::system_director::SystemDirector;
use crate::{Actor, Receive, Respond};
use async_std::task;
use std::fmt::Debug;
pub struct ActorAssistant<A: Actor> {
system_director: SystemDirector,
actors_director: ActorsDirector,
actor_id: A::Id,
}
impl<A: Actor> ActorAssistant<A> {
pub(crate) fn new(
system_director: SystemDirector,
actors_director: ActorsDirector,
actor_id: A::Id,
) -> ActorAssistant<A> {
ActorAssistant {
system_director,
actors_director,
actor_id,
}
}
pub async fn send_to_actor<A2: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
actor_id: A2::Id,
message: M,
) {
self.actors_director.send::<A2, M>(actor_id, message).await
}
pub async fn send_to_all_actors<A2: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
message: M,
) {
self.actors_director.send_to_all::<A2, M>(message).await
}
pub async fn schedule_send_to_actor<A2: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
actor_id: A2::Id,
duration: std::time::Duration,
message: M,
) {
self.system_director
.schedule_send_to_actor::<A2, M>(actor_id, duration, message)
.await
}
pub async fn schedule_send_to_all_actors<A2: Actor + Receive<M>, M: Debug + Send + 'static>(
&self,
duration: std::time::Duration,
message: M,
) {
self.system_director
.schedule_send_to_all_actors::<A2, M>(duration, message)
.await
}
pub async fn call_actor<A2: Actor + Respond<M>, M: Debug + Send + 'static>(
&self,
actor_id: A2::Id,
message: M,
) -> Result<<A2 as Respond<M>>::Response, &str> {
self.actors_director.call::<A2, M>(actor_id, message).await
}
pub async fn send_to_service<S: Service + Listen<M>, M: Debug + Send + 'static>(
&self,
message: M,
) {
self.system_director.send_to_service::<S, M>(message).await
}
pub async fn call_service<S: Service + Serve<M>, M: Debug + Send + 'static>(
&self,
message: M,
) -> Result<<S as Serve<M>>::Response, &str> {
self.system_director.call_service::<S, M>(message).await
}
pub async fn stop(&self) {
self.actors_director
.stop_actor::<A>(self.actor_id.clone())
.await;
}
pub async fn get_id(&self) -> &A::Id {
&self.actor_id
}
pub fn stop_system(&self) {
let system = self.system_director.clone();
task::spawn(async move {
system.stop().await;
});
}
}
impl<A: Actor> Clone for ActorAssistant<A> {
fn clone(&self) -> Self {
ActorAssistant {
actors_director: self.actors_director.clone(),
actor_id: self.actor_id.clone(),
system_director: self.system_director.clone(),
}
}
}
impl<A: Actor> Debug for ActorAssistant<A> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ActorSecretary for {}", std::any::type_name::<A>())
}
}