[][src]Module aktrs::util::event

Utilities for event propagation in local actor systems.

This is implemented by the EventBus actor, which drives event propagation.

Usage

When events are published to the event bus, they are propagated to any active subscribers:

Event buses can also be set up with retention. When an event is published that matches the retention policy of the event bus, it will store the most recent copy of the event. Newly subscribed actors will then receive the most recent copy:

Examples

Basic publish/subscribe:

use aktrs::actor::{Behavior, Behaviors, Pid};
use aktrs::util::{event, EventBus, EventBusPid};

use std::time::Duration;

// Publishes an event to the bus every second.
let publisher =
    |bus: EventBusPid<&'static str>| Behaviors::with_timers(|t| {
        t.start_periodic_timer(Duration::from_secs(1), || ());

        let b = Behaviors::receive_message(move |cx, _msg: ()| {
            let mut bus = bus.clone();
            cx.run_side_effect(async move {
                let _ = bus.tell(event::Command::Publish("hello, world!")).await;
            });
            Ok(Behaviors::same())
        });

        Ok(b.into())
    });

// Prints any event published to the bus.
let subscriber =
    |mut bus: EventBusPid<&'static str>| Behaviors::setup(move |cx| {
        let this = cx.this();
        cx.run_side_effect(async move {
            let _ = bus.tell(event::Command::SubscribeToAll(this)).await;
        });

        let b = Behaviors::receive_message(|_cx, msg: &'static str| {
            println!("Subscriber received a message: {}", msg);
            Ok(Behaviors::same())
        });

        Ok(b.into())
    });

Structs

Classifier

Used by the event bus actor to classify events.

EmptyRetainer

A retainer that does nothing.

EventBus

A built-in actor for propagating events between actors.

Enums

Command

The protocol of messages supported by the event bus actor.

Traits

Retainer

Powers event retention in the event bus actor.

Type Definitions

EventBusPid

A specialized actor reference for event buses.