chip

Chip is a local process registry that plays along with Gleam’s Subject type for referencing erlang processes. It can hold to a set of subjects to later reference individually or dispatch a callback as a group. Will also automatically delist dead processes.

Types

A “chip” used for registration. Check the new function.

pub opaque type Chip(msg, tag, group)

Chip’s internal message type.

pub opaque type Message(msg, tag, group)

An shorter alias for the registry’s Subject.

Sometimes, when building out your system it may be useful to state the Registry’s types.

Example

let assert Ok(registry) = chip.start()
let registry: chip.Registry(Event, Id, Topic)

Which is equivalent to:

let assert Ok(registry) = chip.start()
let registry: process.Subject(chip.Message(Event, Id, Topic))

By specifying the types we can document the kind of registry we are working with.

pub type Registry(msg, tag, group) =
  process.Subject(Message(msg, tag, group))

Functions

pub fn dispatch(
  registry: Subject(Message(a, b, c)),
  callback: fn(Subject(a)) -> d,
) -> Nil

Applies a callback over all registered Subjects.

Example

chip.dispatch(registry, fn(subject) { 
  process.send(subject, message)
})
pub fn dispatch_group(
  registry: Subject(Message(a, b, c)),
  group: c,
  callback: fn(Subject(a)) -> d,
) -> Nil

Applies a callback over a group.

Example

chip.dispatch_group(registry, Pets, fn(subject) { 
  process.send(subject, message)
})
pub fn find(
  registry: Subject(Message(a, b, c)),
  tag: b,
) -> Result(Subject(a), Nil)

Retrieves a tagged subject.

Example

let assert Ok(subject) = chip.find(registry, "Luis")
pub fn group(
  registrant: Chip(a, b, c),
  group: c,
) -> Chip(a, b, c)

Adds the registrant under a group.

Example

chip.new(subject)
|> chip.group(General) 
pub fn new(subject: Subject(a)) -> Chip(a, b, c)

Creates a new registrant value.

Example

chip.new(subject)
pub fn register(
  registry: Subject(Message(a, b, c)),
  registrant: Chip(a, b, c),
) -> Nil

Registers a Registrant.

Example

let assert Ok(registry) = chip.start()

chip.new(subject)
|> chip.register(registry, _)

Registrant may be registered under a tag or group.

let assert Ok(registry) = chip.start()

chip.new(subject)
|> chip.tag("Francisco") 
|> chip.group(Coffee) 
|> chip.register(registry, _)

You may register any subject at any point in time but usually keeping it under the initialization step of your process (like an Actor’s init callback) will keep things organized and tidy.

pub fn start() -> Result(Subject(Message(a, b, c)), StartError)

Starts the registry.

Example

> chip.start()
pub fn tag(registrant: Chip(a, b, c), tag: b) -> Chip(a, b, c)

Adds a unique tag to a registrant, it will overwrite any previous subject under the same tag.

Example

chip.new(subject)
|> chip.tag("Luis") 
Search Document