gleam/otp/actor

Types

ErlangStartResult

An Erlang supervisor compatible process start result.

pub type ErlangStartResult =
  Result(Pid, StartError)

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.

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, StartError)

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