chip/group

This is a local process registry that groups Subjects under a name to later reference or broadcast messages to them.

Types

This is the message type used internally by group.

When building out your system it may be useful to state the group types on startup. For example:

let assert Ok(server) = chat.start()
let server: process.Subject(Chat)

type Topic {
  General
  OffTopic
  Cats
}

let assert Ok(registry) = group.start()
let registry: process.Subject(group.Message(Topic, Chat))

By specifying the types we can document the kind of registry we are working with; in the example above we can tell we’re creating different “chat servers” under different topic groups.

pub opaque type Message(group, msg)

Functions

pub fn dispatch(
  registry: Subject(Message(a, b)),
  group: a,
  callback: fn(Subject(b)) -> c,
) -> Nil

Executes a callback for all Subjects under a named group.

Example

> group.dispatch(registry, "group-a", fn(subject) { 
>   process.send(subject, Message(data))
> })
Nil
pub fn members(
  registry: Subject(Message(a, b)),
  group: a,
) -> List(Subject(b))

Looks up Subjects under a named group.

Example

> group.find(registry, "group-a") 
[subject_1, subject_2, subject_3]
pub fn register(
  registry: Subject(Message(a, b)),
  subject: Subject(b),
  group: a,
) -> Nil

Registers a Subject under a shared name.

Example

> group.register(registry, process.new_subject(), "group-a")
Nil
pub fn start() -> Result(Subject(Message(a, b)), StartError)

Starts the registry.

Example

> group.start()
Ok(registry)
Search Document