use std::sync::Arc;
use tokio::sync::mpsc::Receiver;
use crate::{
Actor, ActorConfig, ActorId, Context, Envelope, Event, Result, Subscribe, Supervisor, Topic,
internal::Subscription,
};
#[must_use = "actor is not registered until .build() is called"]
pub struct ActorBuilder<'a, E: Event, T: Topic<E>, A: Actor<Event = E>> {
supervisor: &'a mut Supervisor<E, T>,
actor: A,
ctx: Context<A::Event>,
config: ActorConfig,
topics: Subscription<T>,
receiver: Receiver<Arc<Envelope<E>>>,
}
impl<'a, E: Event, T: Topic<E>, A: Actor<Event = E>> ActorBuilder<'a, E, T, A> {
pub(crate) fn new(
supervisor: &'a mut Supervisor<E, T>,
actor: A,
ctx: Context<A::Event>,
receiver: Receiver<Arc<Envelope<E>>>,
) -> Self {
let config = ActorConfig::new(supervisor.config());
Self {
supervisor,
ctx,
actor,
config,
topics: Subscription::None,
receiver,
}
}
pub fn topics<S>(mut self, topics: S) -> Self
where
S: Into<Subscribe<E, T>>,
{
self.topics = topics.into().0;
self
}
pub fn config<C>(mut self, config: C) -> Self
where
C: Into<ActorConfig>,
{
self.config = config.into();
self
}
pub fn with_config<F>(mut self, f: F) -> Self
where
F: FnOnce(ActorConfig) -> ActorConfig,
{
self.config = f(self.config);
self
}
pub fn channel_capacity(mut self, capacity: usize) -> Self {
self.config = self.config.with_channel_capacity(capacity);
self
}
pub fn build(self) -> Result<ActorId> {
self.supervisor.register_actor(
self.ctx,
self.actor,
self.topics,
self.config,
self.receiver,
)
}
}