use std::any::{Any, TypeId};
use hashbrown::HashMap;
use nohash_hasher::BuildNoHashHasher;
pub mod trigger;
#[cfg(feature = "derive")]
pub use hui_derive::Signal;
pub trait Signal: Any {}
pub struct SignalStore {
sig: HashMap<TypeId, Vec<Box<dyn Any>>, BuildNoHashHasher<u64>>
}
impl SignalStore {
pub(crate) fn new() -> Self {
Self {
sig: Default::default(),
}
}
fn internal_store<T: Signal + 'static>(&mut self) -> &mut Vec<Box<dyn Any>> {
let type_id = TypeId::of::<T>();
self.sig.entry(type_id).or_default()
}
pub fn add<T: Signal + 'static>(&mut self, sig: T) {
let type_id = TypeId::of::<T>();
if let Some(v) = self.sig.get_mut(&type_id) {
v.push(Box::new(sig));
} else {
self.sig.insert(type_id, vec![Box::new(sig)]);
}
}
pub(crate) fn drain<T: Signal + 'static>(&mut self) -> impl Iterator<Item = T> + '_ {
self.internal_store::<T>()
.drain(..)
.map(|x| *x.downcast::<T>().unwrap()) }
pub(crate) fn clear(&mut self) {
self.sig.clear();
}
}