use super::*;
impl EventBus {
pub fn emit<ErrorType>(event: ErrorType)
where
ErrorType: CustomEvent,
{
let id: SignalId = with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.ensure_event_channel::<ErrorType>(),
SignalId::default(),
);
let channel: Signal<(u64, Option<ErrorType>)> = Signal::from_raw_id(id);
let _: bool = {
if let Some(mut updated_value) = channel.try_get_untracked() {
{
let slot: &mut (u64, Option<ErrorType>) = &mut updated_value;
slot.0 = slot.0.wrapping_add(1);
slot.1 = Some(event);
}
channel.set(updated_value)
} else {
false
}
};
}
#[must_use]
pub fn on<ErrorType, CallbackFn>(mut handler: CallbackFn) -> Effect
where
ErrorType: CustomEvent,
CallbackFn: FnMut(&ErrorType) + 'static,
{
let id: SignalId = with_runtime(
|runtime_ref: &mut ReactiveRuntime| runtime_ref.ensure_event_channel::<ErrorType>(),
SignalId::default(),
);
let channel: Signal<(u64, Option<ErrorType>)> = Signal::from_raw_id(id);
let seen_seq: Rc<Cell<u64>> = Rc::new(Cell::new(channel.get_untracked().0));
Effect::new(move || {
let (seq, payload): (u64, Option<ErrorType>) = channel.get();
if seq != seen_seq.replace(seq)
&& let Some(ui_event) = payload.as_ref()
{
handler(ui_event);
}
})
}
}