[][src]Crate revent

Event broker library for Rust.

Implements a synchronous transitive event broker that does not violate mutability constraints. It does so by performing DAG traversals to ensure that no signal chains are able to form a loop.

What is an event broker?

An event broker is a bag of objects and a bunch of "signals". Each object decides which signals to listen to.

Each object (also called subscriber) is notified when its subscribed signal is emitted. A subscriber may during notification processing emit yet another signal into the broker, and so on.

use revent::{hub, Shared, Subscriber};

// Construct a trait for some type of event.
trait MyEvent {
    fn my_event(&mut self);
}

// Create an event hub.
hub! {
    Hub {
        event: dyn MyEvent,
    }
}

// Construct a hub object.
let hub = Hub::default();

// Implement a subscriber to some event.
struct X;
impl MyEvent for X {
    fn my_event(&mut self) {
        println!("Hello world");
    }
}
impl Subscriber<Hub> for X {
    type Input = ();
    fn build(_: Hub, input: Self::Input) -> Self {
        Self
    }
    fn subscribe(hub: &Hub, shared: Shared<Self>) {
        hub.event.subscribe(shared);
    }
}

// Create an instance of X in the hub.
hub.subscribe::<X>(());

// Now emit an event into the topic.
hub.event.emit(|x| {
    x.my_event();
});

Macros

hub

Generate an event hub and its associated boilerplate code.

Structs

Shared

An opaque struct containing a shared reference to a subscriber.

Topic

An event channel for a certain class of subscribers.

Traits

Subscriber

Subscriber to an event hub.