fresnel/fan_out
An actor that distributes published messages to all subscribed subject. Subjects can be dynamically subscribed or unsubscribed from the publisher.
Types
Messages that can be sent to the fan_out publisher actor.
pub type Message(e) {
Subscribe(Subject(e))
Unsubscribe(Subject(e))
Publish(e)
}
Constructors
-
Subscribe(Subject(e))
Add a new subscriber that will begin receiving events
-
Unsubscribe(Subject(e))
Remove a subscriber from receiving published messages
-
Publish(e)
Publish an event to all subscribers
Functions
pub fn handler(
msg: Message(a),
subscribers: List(Subject(a)),
) -> Next(Message(a), List(Subject(a)))
The handler to start the round robin actor
Examples:
import fresnel/fan_out
import gleam/otp/actor
import gleam/erlang/process
import gleam/io
type Event {
Lights
Camera
Action
}
fn subscriber(event: Event, id: String) -> actor.Next(Event, String) {
case event {
Lights -> {
io.println(id<>": lights")
actor.continue(id)
}
Camera -> {
io.println(id<>": camera")
actor.continue(id)
}
Action -> {
io.println(id<>": action!")
actor.continue(id)
}
}
}
pub fn main() {
let assert Ok(publisher) = actor.start([], fan_out.handler)
let assert Ok(listener1) = actor.start("1", subscriber)
let assert Ok(listener2) = actor.start("2", subscriber)
process.send(publisher, Lights)
process.send(publisher, Camera)
process.send(publisher, Action)
}