Chip - A subject registry library

Package Version Hex Docs

Chip is a performant local registry that can hold to a set of subjects to later retrieve or dispatch tasks to.

Example

Lets assemble a simple counter actor:

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

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

fn loop(message: Message, count: Int) {
  case message {
    Inc -> {
      actor.continue(count + 1)
    }

    Current(client) -> {
      process.send(client, count)
      actor.continue(count)
    }
  }
}

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

import chip
import gleam/otp/actor

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

  let assert Ok(counter_1) = actor.start(0, loop)
  let assert Ok(counter_2) = actor.start(0, loop)
  let assert Ok(counter_3) = actor.start(0, loop)

  chip.register(registry, chip.new(counter_1) |> chip.tag(1))
  chip.register(registry, chip.new(counter_2) |> chip.tag(2))
  chip.register(registry, chip.new(counter_3) |> chip.tag(3))
  
  process.sleep_forever()
}

Later, we may retrieve a member:

let assert Ok(counter) = chip.find(registry, 2)
let assert 0 = actor.call(counter, Current(_), 10)

Or dispatch a task to all members or group:

chip.dispatch(registry, fn(counter) {
  actor.send(counter, Inc)
}) 

let assert Ok(counter) = chip.find(registry, 2)
let assert 1 = actor.call(counter, Current(_), 10)

Chip will also automatically delist dead processes.

The road towards V1

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

Couple of adjustments and cleanup left for V1!

Tentative features

Previous Art and other Gleam registry alternatives

This registry takes and combines some ideas from Elixir’s Registry, Erlang’s pg and Syn.

Singularity is a gleam library that offers registry capabilities but focusing more on singleton actors, therefore it is better suited for keeping track of actors that need to be passed around as configuration through your app.

Installation

gleam add chip
Search Document