use crate::{
Slot,
SlotProxy,
};
#[derive(Default)]
pub struct Signal<T> {
listeners: Vec<Box<dyn FnMut(&T)>>,
senders: Vec<SlotProxy<T>>,
}
impl<T: 'static + Clone> Signal<T> {
pub fn new() -> Signal<T> {
return Signal {
listeners: Vec::new(),
senders: Vec::new(),
};
}
pub fn connect_func<F: 'static + FnMut(&T)>(
&mut self,
f: F,
) {
self.listeners.push(Box::new(f));
}
pub fn connect_slot(
&mut self,
slot: &dyn Slot<T>,
) {
self.senders.push(slot.proxy());
}
pub fn emit(
&mut self,
t: T,
) {
self.listeners.iter_mut().for_each(|l| {
l.as_mut()(&t);
});
self.senders.iter().for_each(|s| {
match s.exec_for(t.clone()) {
true => trace!("sended to slot"),
false => warn!("send to slot err"),
}
});
}
pub fn has_connections(&self) -> bool {
return (self.listeners.len() + self.senders.len()) > 0;
}
}