gleam/otp/actor

Types

ErlangStartResult

An Erlang supervisor compatible process start result.

If you wish to convert this into a StartResult compatible with Gleam supervisors see the from_erlang_start_result and wrap_erlang_starter functions.

pub type ErlangStartResult =
  Result(Pid, Dynamic)

InitResult

The type used to indicate whether an actor has started successfully or not.

pub type InitResult(state, message) {
  Ready(state: state, receiver: Option(Receiver(message)))
  Failed(ExitReason)
}

Constructors

  • Ready(state: state, receiver: Option(Receiver(message)))

    The actor has successfully initialised. The actor can start handling messages and actor's channel sender can be returned to the parent process.

  • Failed(ExitReason)

    The actor has failed to initialise. The actor shuts down and an error is returned to the parent process.

Next

The type used to indicate what to do after handling a message.

pub type Next(state) {
  Continue(state)
  Stop(ExitReason)
}

Constructors

  • Continue(state)

    Continue handling messages.

  • Stop(ExitReason)

    Stop handling messages and shut down.

Spec

This data structure holds all the values required by the start_spec function in order to create an actor.

If you do not need to configure the initialisation behaviour of your actor consider using the start function.

pub type Spec(state, msg) {
  Spec(
    init: fn() -> InitResult(state, msg),
    init_timeout: Int,
    loop: fn(msg, state) -> Next(state),
  )
}

Constructors

  • Spec( init: fn() -> InitResult(state, msg), init_timeout: Int, loop: fn(msg, state) -> Next(state), )

StartError

pub type StartError {
  InitTimeout
  InitFailed(ExitReason)
  InitCrashed(Dynamic)
}

Constructors

  • InitTimeout
  • InitFailed(ExitReason)
  • InitCrashed(Dynamic)

StartResult

The result of starting a Gleam actor.

This type is compatible with Gleam supervisors. If you wish to convert it to a type compatible with Erlang supervisors see the ErlangStartResult type and erlang_start_result function.

pub type StartResult(msg) =
  Result(Sender(msg), StartError)

Functions

call

pub fn call(
  receiver: Sender(a),
  make_message: fn(Sender(b)) -> a,
  timeout: Int,
) -> b

Send a synchronous message and wait for a response from the receiving process.

If a reply is not received within the given timeout then the sender process crashes. If you wish receive a Result rather than crashing see the process.try_call function.

This is a re-export of process.call, for the sake of convenience.

from_erlang_start_result

pub fn from_erlang_start_result(
  start: Result(Pid, a),
) -> Result(Sender(b), StartError)

Processes written in Erlang or other BEAM languages use bare processes rather than channels, so the value returned from their process start functions are not compatible with Gleam supervisors. This function can be used to wrap the return value so it can be used with a Gleam supervisor.

pid

pub fn pid(sender: Sender(a)) -> Pid

Get the pid of the receiver process for a sender.

send

pub fn send(channel: Sender(a), msg: a) -> Sender(a)

Send a message over a given channel.

This is a re-export of process.send, for the sake of convenience.

start

pub fn start(
  state: a,
  loop: fn(b, a) -> Next(a),
) -> Result(Sender(b), StartError)

Start an actor with a given initial state and message handling loop function.

This function returns a Result but it will always be Ok so it is safe to use with assert if you are not starting this actor as part of a supervision tree.

If you wish to configure the initialisation behaviour of a new actor see the Spec record and the start_spec function.

start_spec

pub fn start_spec(
  spec: Spec(a, b),
) -> Result(Sender(b), StartError)

Start an actor from a given specification. If the actor's init function returns an error or does not return within init_timeout then an error is returned.

If you do not need to specify the initialisation behaviour of your actor consider using the start function.

to_erlang_start_result

pub fn to_erlang_start_result(
  res: Result(Sender(a), StartError),
) -> Result(Pid, Dynamic)

Convert a Gleam actor start result into an Erlang supervisor compatible process start result.

wrap_erlang_starter

pub fn wrap_erlang_starter(
  start: fn() -> Result(Pid, a),
) -> fn() -> Result(Sender(b), StartError)

Processes written in Erlang or other BEAM languages use bare processes rather than channels, so the value returned from their process start functions are not compatible with Gleam supervisors. This function can be used to wrap the start function so it can be used with a Gleam supervisor.