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.

Message(name, group, msg) generics correspond to the types that the registry will use to manage unique names, group names and Subject messages. When building your system it is useful to state these on startup. For example:

type Group {
  A
  B
  C
}

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 groups A, B or C.

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))
> })
Nil
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_1, subject_2, subject_3]
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
pub fn start() -> Result(Subject(Message(a, b, c)), StartError)

Starts the registry.

Example

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