[−][src]Crate revent
Synchronous event system.
What is an event system?
An event system is a set of slots which contain objects. A signal is emitted on a slot, which will call each object in the slot. Invoked objects can then send more signals to different slots.
Synchronous
Revent's events are synchronous, meaning that emitting an event will immediately process all handlers in a slot. Once the function call returns, it is guaranteed that all listeners have been called.
Example
use revent::{Manager, Named, Node, Null, Slot, Subscriber}; use std::{cell::RefCell, rc::Rc}; trait BasicSignal {} struct Hub { basic_slot: Slot<dyn BasicSignal>, mng: Rc<RefCell<Manager>>, } impl Hub { fn new() -> Self { let mng = Rc::new(RefCell::new(Manager::default())); Self { basic_slot: Slot::new("basic_slot", mng.clone()), mng, } } } impl Node for Hub { fn manager(&self) -> &Rc<RefCell<Manager>> { &self.mng } } // --- struct MySubscriber; impl Subscriber<Hub> for MySubscriber { type Input = (); type Node = Null; fn create(_: Self::Input, _: Self::Node) -> Self { Self } fn register(hub: &mut Hub, item: Rc<RefCell<Self>>) { hub.basic_slot.register(item); } } impl Named for MySubscriber { const NAME: &'static str = "MySubscriber"; } impl BasicSignal for MySubscriber {} // --- let mut hub = Hub::new(); let item = hub.subscribe::<MySubscriber>(()); hub.basic_slot.emit(|x| { println!("Called for each subscriber"); }); hub.unsubscribe(&item);
Mutable cycles
Revent performs cycle detection in subscribe and ensures that no system exists in which we can create double mutable borrows.
Structs
Grapher | Wrapper around a Manager that generates a graph. |
Manager | Inspects how various Subscribers use Slots. |
Null | Null |
Slot | List of Subscribers to a signal |
Traits
Named | Attach a name to a type. |
Node | A collection of Slots to which Subscribers can subscribe. |
Subscriber | Describes a subscriber that can subscribe to Node. |