chip
Chip is a gleam process registry that plays along the Gleam Erlang Subject type.
It lets tag subjects under a name or group to later reference them. Will also automatically delist dead processes.
Types
This is the message type used internally by Chip.
Its generics name, group, and msg correspond to whatever types we do want to assign to the
registry when initializing. A Chip instance only accepts `Subject’s of the same type so it is
sometimes useful to state the types on startup. For example:
type Group {
GroupA
GroupB
GroupC
}
> let assert Ok(registry) = chip.start()
> let registry: process.Subject(chip.Message(String, Group, Chat))
By specifying the types we can document the kind of registry we are working with. For example the
registry above lets us tag subjects that use the Subject(Chat) type; it lets us tag individual
subjects through stringified names; finally lets us group subjects into a group A, B or C.
Of course we can always rely on gleam’s type inference to do the typing for us.
pub opaque type Message(name, group, msg)
Functions
pub fn broadcast(
registry: Subject(Message(a, b, c)),
group: b,
callback: fn(Subject(c)) -> d,
) -> Nil
Executes a callback for all Subjects under a named group.
Example
> chip.broadcast(registry, "group-a", fn(subject) {
> process.send(subject, Message(data))
> })
pub fn find(
registry: Subject(Message(a, b, c)),
name: a,
) -> Result(Subject(c), Nil)
Looks up a uniquely named Subject.
Example
> chip.find(registry, "my-subject")
Ok(subject)
pub fn group(
registry: Subject(Message(a, b, c)),
subject: Subject(c),
group: b,
) -> Nil
Registers a Subject under a shared name.
Example
> chip.group(registry, process.new_subject(), "group-a")
Nil
pub fn members(
registry: Subject(Message(a, b, c)),
group: b,
) -> List(Subject(c))
Looks up Subjects under a named group.
Example
> chip.find(registry, "group-a")
[subject]
pub fn register(
registry: Subject(Message(a, b, c)),
subject: Subject(c),
name: a,
) -> Nil
Registers a Subject under a unique name.
Example
> chip.register(registry, process.new_subject(), "my-subject")
Nil