use crate::hotswap::ActorFactory;
use crate::hotswap::types::{WorkerError, WorkerMessage, WorkerResponder};
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::atomic::{AtomicU64, Ordering};
pub struct DynamicSupervisor<T, R>
where
T: Send + 'static,
R: Send + 'static,
{
pub module_name: String,
factory: std::sync::Arc<ActorFactory>,
work_counter: AtomicU64,
#[allow(dead_code)]
pending: HashMap<u64, WorkerResponder<R>>,
_phantom: PhantomData<(T, R)>,
}
impl<T, R> DynamicSupervisor<T, R>
where
T: Send + 'static,
R: Send + 'static,
{
pub fn new(module_name: &str, factory: std::sync::Arc<ActorFactory>) -> Self {
Self {
module_name: module_name.to_string(),
factory,
work_counter: AtomicU64::new(0),
pending: HashMap::new(),
_phantom: PhantomData,
}
}
pub fn prepare_work_spawn(
&self,
initial_state: Vec<u8>,
) -> Result<std::sync::Arc<crate::hotswap::types::ActorVTable>, crate::error::RuntimeError>
{
let work_id = self.work_counter.fetch_add(1, Ordering::Relaxed);
let _ = work_id;
let spec = crate::hotswap::types::ActorSpec {
module_name: self.module_name.clone(),
initial_state,
};
self.factory.prepare_dynamic_spawn(&spec)
}
pub fn current_version(&self) -> Option<String> {
self.factory.current_version(&self.module_name)
}
pub fn runtime_sender(&self) -> &crate::runtime::RuntimeSender {
self.factory.runtime_sender()
}
}
#[allow(dead_code)]
type _Silence = WorkerMessage<(), ()>;
#[allow(dead_code)]
type _Silence2 = WorkerError;