use std::future::Future;
use std::pin::Pin;
use std::sync::OnceLock;
use crate::error::{Error, Result};
#[derive(Debug, Clone)]
pub struct DeletionRequest {
pub run_id: String,
pub root_table: String,
pub root_record_id: String,
pub actor_json: serde_json::Value,
}
type DispatchFn =
dyn Fn(DeletionRequest) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> + Send + Sync;
static DISPATCHER: OnceLock<Box<DispatchFn>> = OnceLock::new();
pub fn is_deletion_dispatcher_registered() -> bool {
DISPATCHER.get().is_some()
}
pub fn register_deletion_dispatcher(f: Box<DispatchFn>) {
let _ = DISPATCHER.set(f);
}
pub fn register_noop_deletion_dispatcher_for_tests() {
let noop: Box<DispatchFn> = Box::new(|_| Box::pin(async move { Ok(()) }));
let _ = DISPATCHER.set(noop);
}
pub async fn dispatch(req: DeletionRequest) -> Result<()> {
match DISPATCHER.get() {
Some(f) => (f)(req).await,
None => Err(Error::Internal(
"Deletion dispatcher not registered; register a host dispatcher from server bootstrap"
.into(),
)),
}
}