use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, OnceLock, RwLock};
use crate::core::Model;
pub type ReceiverFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ReceiverId(u64);
#[derive(Debug, Clone, Copy)]
pub struct PostSaveContext {
pub created: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum SignalKind {
PreSave,
PostSave,
PreDelete,
PostDelete,
}
type ReceiverEntry = (ReceiverId, Box<dyn Any + Send + Sync>);
type Bag = Vec<ReceiverEntry>;
fn registry() -> &'static RwLock<HashMap<(TypeId, SignalKind), Bag>> {
static REG: OnceLock<RwLock<HashMap<(TypeId, SignalKind), Bag>>> = OnceLock::new();
REG.get_or_init(|| RwLock::new(HashMap::new()))
}
fn next_id() -> ReceiverId {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(1);
ReceiverId(COUNTER.fetch_add(1, Ordering::Relaxed))
}
fn insert_receiver<R: Any + Send + Sync>(key: (TypeId, SignalKind), receiver: R) -> ReceiverId {
let id = next_id();
let mut reg = registry().write().expect("signals registry poisoned");
reg.entry(key).or_default().push((id, Box::new(receiver)));
id
}
fn remove_receiver(key: (TypeId, SignalKind), id: ReceiverId) -> bool {
let mut reg = registry().write().expect("signals registry poisoned");
let Some(bag) = reg.get_mut(&key) else { return false };
let before = bag.len();
bag.retain(|(rid, _)| *rid != id);
bag.len() != before
}
fn snapshot<R: Any + Send + Sync + Clone>(key: (TypeId, SignalKind)) -> Vec<R> {
let reg = registry().read().expect("signals registry poisoned");
let Some(bag) = reg.get(&key) else { return Vec::new() };
bag.iter()
.filter_map(|(_, b)| b.downcast_ref::<R>().cloned())
.collect()
}
type SimpleReceiver<T> = Arc<dyn Fn(Arc<T>) -> ReceiverFuture + Send + Sync>;
type PostSaveReceiver<T> = Arc<dyn Fn(Arc<T>, PostSaveContext) -> ReceiverFuture + Send + Sync>;
pub fn connect_pre_save<T, F, Fut>(receiver: F) -> ReceiverId
where
T: Model + Clone + 'static,
F: Fn(Arc<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let boxed: SimpleReceiver<T> = Arc::new(move |instance| Box::pin(receiver(instance)));
insert_receiver((TypeId::of::<T>(), SignalKind::PreSave), boxed)
}
pub fn disconnect_pre_save<T: Model + 'static>(id: ReceiverId) -> bool {
remove_receiver((TypeId::of::<T>(), SignalKind::PreSave), id)
}
pub async fn send_pre_save<T: Model + Clone + 'static>(instance: &T) {
let receivers: Vec<SimpleReceiver<T>> =
snapshot::<SimpleReceiver<T>>((TypeId::of::<T>(), SignalKind::PreSave));
let arc = Arc::new(instance.clone());
for r in receivers {
r(arc.clone()).await;
}
}
pub fn connect_post_save<T, F, Fut>(receiver: F) -> ReceiverId
where
T: Model + Clone + 'static,
F: Fn(Arc<T>, PostSaveContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let boxed: PostSaveReceiver<T> =
Arc::new(move |instance, ctx| Box::pin(receiver(instance, ctx)));
insert_receiver((TypeId::of::<T>(), SignalKind::PostSave), boxed)
}
pub fn disconnect_post_save<T: Model + 'static>(id: ReceiverId) -> bool {
remove_receiver((TypeId::of::<T>(), SignalKind::PostSave), id)
}
pub async fn send_post_save<T: Model + Clone + 'static>(instance: &T, ctx: PostSaveContext) {
let receivers: Vec<PostSaveReceiver<T>> =
snapshot::<PostSaveReceiver<T>>((TypeId::of::<T>(), SignalKind::PostSave));
let arc = Arc::new(instance.clone());
for r in receivers {
r(arc.clone(), ctx).await;
}
}
pub fn connect_pre_delete<T, F, Fut>(receiver: F) -> ReceiverId
where
T: Model + Clone + 'static,
F: Fn(Arc<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let boxed: SimpleReceiver<T> = Arc::new(move |instance| Box::pin(receiver(instance)));
insert_receiver((TypeId::of::<T>(), SignalKind::PreDelete), boxed)
}
pub fn disconnect_pre_delete<T: Model + 'static>(id: ReceiverId) -> bool {
remove_receiver((TypeId::of::<T>(), SignalKind::PreDelete), id)
}
pub async fn send_pre_delete<T: Model + Clone + 'static>(instance: &T) {
let receivers: Vec<SimpleReceiver<T>> =
snapshot::<SimpleReceiver<T>>((TypeId::of::<T>(), SignalKind::PreDelete));
let arc = Arc::new(instance.clone());
for r in receivers {
r(arc.clone()).await;
}
}
pub fn connect_post_delete<T, F, Fut>(receiver: F) -> ReceiverId
where
T: Model + Clone + 'static,
F: Fn(Arc<T>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
let boxed: SimpleReceiver<T> = Arc::new(move |instance| Box::pin(receiver(instance)));
insert_receiver((TypeId::of::<T>(), SignalKind::PostDelete), boxed)
}
pub fn disconnect_post_delete<T: Model + 'static>(id: ReceiverId) -> bool {
remove_receiver((TypeId::of::<T>(), SignalKind::PostDelete), id)
}
pub async fn send_post_delete<T: Model + Clone + 'static>(instance: &T) {
let receivers: Vec<SimpleReceiver<T>> =
snapshot::<SimpleReceiver<T>>((TypeId::of::<T>(), SignalKind::PostDelete));
let arc = Arc::new(instance.clone());
for r in receivers {
r(arc.clone()).await;
}
}
pub fn clear_all() {
registry().write().expect("signals registry poisoned").clear();
}
pub fn receiver_count<T: Model + 'static>() -> usize {
let reg = registry().read().expect("signals registry poisoned");
let id = TypeId::of::<T>();
[SignalKind::PreSave, SignalKind::PostSave, SignalKind::PreDelete, SignalKind::PostDelete]
.iter()
.map(|kind| reg.get(&(id, *kind)).map_or(0, Vec::len))
.sum()
}