pub struct EventBus<E, V> { /* private fields */ }Expand description
An event bus that can be cloned and shared across threads. If you do not
need to share the bus across threads use unsync::EventBus which is
more efficient in terms of performance since it doens’t need to hold
locks on resources
§Example
use tram::{prelude::*, sync::EventBus};
use std::{cell::RefCell, rc::Rc};
#[derive(PartialEq, Eq, Hash)]
enum EventType {
Start,
Stop
}
#[derive(Debug, PartialEq)]
enum Status {
Stopped,
Started
}
let bus: EventBus<EventType, ()> = EventBus::unbound();
let status = Rc::new(RefCell::new(Status::Stopped));
let status_closure = Rc::clone(&status);
bus.on(EventType::Start, move |_bus, _| {
*status_closure.borrow_mut() = Status::Started;
})
.expect("Failed to listen for this event");
bus.emit(EventType::Start).expect("Failed to emit");
assert_eq!(*status.borrow(), Status::Started);
assert_eq!(bus.event_count(), 1);Implementations§
Source§impl<E, V> EventBus<E, V>
impl<E, V> EventBus<E, V>
Sourcepub fn disconnected(&self) -> bool
pub fn disconnected(&self) -> bool
Returns true if this bus has exausted its allowed max number of emits
pub fn event_count(&self) -> usize
Trait Implementations§
Source§impl<E, V> EventEmitter<E, V> for EventBus<E, V>
impl<E, V> EventEmitter<E, V> for EventBus<E, V>
impl<E, V> Send for EventBus<E, V>where
E: Send,
impl<E, V> Sync for EventBus<E, V>where
E: Send,
Auto Trait Implementations§
impl<E, V> Freeze for EventBus<E, V>
impl<E, V> !RefUnwindSafe for EventBus<E, V>
impl<E, V> Unpin for EventBus<E, V>
impl<E, V> !UnwindSafe for EventBus<E, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more