Package Version Hex Docs

////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.


Lets assemble the pieces to build a simple counter actor:

pub type Message {
  Current(client: process.Subject(Int))

fn loop(message: Message, count: Int) {
  case message {
    Inc -> {
      actor.Continue(count + 1, option.None)

    Current(client) -> {
      process.send(client, count)
      actor.Continue(count, option.None)

    Stop -> {

We start our registry and create new instances of a counter:

import gleam/erlang/process
import chip

pub fn main() {
  let assert Ok(registry) = chip.start()

  let assert Ok(counter_1) = actor.start(1, loop)
  let assert Ok(counter_2) = actor.start(2, loop)
  let assert Ok(counter_3) = actor.start(3, loop), counter_1, "counters"), counter_2, "counters"), counter_3, "counters")

Later we can lookup for all subjects under the group and send messages:

chip.broadcast(registry, "counters", fn(counter) {
  actor.send(counter, Inc)

Or retrieve the current state of our subjects:

let assert [2, 3, 4] =  
  chip.members(registry, "counters")
  |>, Current(_), 10))
  // Subject maybe be retrieved out of order so we do it explicitly
  |> list.sort(

Feature-wise this is near beign complete. Still planning to integrate:


gleam add chip
Search Document