chip
Chip is a local subject registry that can store Gleam process subjects as part of a group. Will also automatically delist dead processes.
Types
An option passed to chip.start
to make the registry available through a name.
pub type Named {
Named(String)
Unnamed
}
Constructors
-
Named(String)
-
Unnamed
Functions
pub fn from(name: String) -> Result(Subject(Message(a, b)), Nil)
Retrieves a previously named registry.
Example
This function can be useful when there is no registry subject in scope.
let _ = chip.start(chip.Named("sessions"))
let assert Ok(registry) = chip.from("sessions")
Be mindful that using from
means you lose type safety as the original group
and
subject message
will be not inferred from it. To circunvent this it is possible
to manually specify the type signature.
let assert Ok(registry) = chip.from("sessions")
// specify through a typed variable
let registry: chip.Registry(Message, Groups)
// specify through a helper function
fn get_session(name: String) -> chip.Registry(Message, Groups) {
case chip.from("sessions") {
Ok(registry) -> registry
Error(Nil) -> panic as "session is not available"
}
}
pub fn members(
registry: Subject(Message(a, b)),
group: b,
timeout: Int,
) -> List(Subject(a))
Retrieves all subjects from a given group, order of retrieved subjects is not guaranteed.
Example
let assert Ok(registry) = chip.start(chip.Unnamed)
chip.register(registry, GroupA, subject)
chip.register(registry, GroupB, subject)
chip.register(registry, GroupA, subject)
let assert [_, _] = chip.members(registry, GroupA, 50)
let assert [_] = chip.members(registry, GroupB, 50)
pub fn register(
registry: Subject(Message(a, b)),
group: b,
subject: Subject(a),
) -> Nil
Registers a subject under a group.
Example
let assert Ok(registry) = chip.start(chip.Unnamed)
chip.register(registry, GroupA, subject)
chip.register(registry, GroupB, subject)
chip.register(registry, GroupC, subject)
A subject may be registered under multiple groups but it may only be registered one time on each group.
pub fn start(
named: Named,
) -> Result(Subject(Message(a, b)), StartError)
Starts the registry.
Example
The registry may be started in an unnamed fashion.
> let assert Ok(registry) = chip.start(chip.Unnamed)
As a convenience, it is also possible to start a named registry which can be retrieved later by using from.
> let _ = chip.start(chip.Named("sessions"))