use std::sync::Arc;
use hello_egui_utils::MaybeSend;
use parking_lot::Mutex;
#[cfg(not(target_arch = "wasm32"))]
use type_map::concurrent::TypeMap;
#[cfg(target_arch = "wasm32")]
use type_map::TypeMap;
use crate::broadcast::{Broadcast, BroadcastReceiver};
#[derive(Debug, Clone, Default)]
pub struct TypeBroadcast {
broadcasts: Arc<Mutex<TypeMap>>,
}
impl TypeBroadcast {
pub fn new() -> Self {
Self::default()
}
pub fn subscribe<T: MaybeSend + 'static>(&self) -> BroadcastReceiver<T> {
self.broadcasts
.lock()
.entry()
.or_insert_with(|| Broadcast::new())
.subscribe()
}
pub fn send<T: MaybeSend + Clone + 'static>(&self, message: T) {
let mut broadcasts = self.broadcasts.lock();
let entry = broadcasts.entry().or_insert_with(|| Broadcast::new());
entry.send(message);
}
}