chip

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.

Example

Lets assemble the pieces to build a simple counter actor:

import gleam/erlang/process
import gleam/otp/actor

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

pub fn increment(counter) {
  actor.send(counter, Inc)
}

pub fn current(counter) {
  actor.call(counter, Current(_), 10)
}

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)
    }
  }
}

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

import gleam/otp/actor
import chip/group

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)

  group.register(registry, counter_1, "counters")
  group.register(registry, counter_2, "counters")
  group.register(registry, counter_3, "counters")
  
  process.sleep_forever()
}

Later, we may retrieve members for a group:

let assert [a, b, c] = group.members(registry, GroupA)
let assert 6 = counter.current(a) + counter.current(b) + counter.current(c)

Or dispatch a command for each Subject::

group.dispatch(registry, "counters", fn(counter) {
  actor.increment(counter)
}) 

let assert [a, b, c] = group.members(registry, GroupA)
let assert 9 = counter.current(a) + counter.current(b) + counter.current(c)

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

Installation

gleam add chip
Search Document