glixir/agent

Types

Opaque type representing an Agent process

pub opaque type Agent

Errors that can occur with Agents

pub type AgentError {
  StartError(reason: String)
  Timeout
  AgentDown
  DecodeError(String)
}

Constructors

  • StartError(reason: String)
  • Timeout
  • AgentDown
  • DecodeError(String)

Values

pub fn cast(agent: Agent, fun: fn(a) -> a) -> Nil

Update the state asynchronously

pub fn get(
  agent: Agent,
  fun: fn(a) -> b,
  decoder: decode.Decoder(b),
) -> Result(b, AgentError)

Get the current state Requires a decoder for the expected type b.

pub fn get_and_update(
  agent: Agent,
  fun: fn(a) -> #(b, a),
  decoder: decode.Decoder(b),
) -> Result(b, AgentError)

Get and update in one operation Requires a decoder for the expected type b.

pub fn get_timeout(
  agent: Agent,
  fun: fn(a) -> b,
  timeout: Int,
  decoder: decode.Decoder(b),
) -> Result(b, AgentError)

Get with custom timeout Requires a decoder for the expected type b.

pub fn pid(agent: Agent) -> process.Pid

Get the PID of an Agent

pub fn start(initial_fun: fn() -> a) -> Result(Agent, AgentError)

Start a new Agent with initial state

pub fn start_named(
  name: String,
  initial_fun: fn() -> a,
) -> Result(Agent, AgentError)

Start an Agent with a name

pub fn stop(agent: Agent) -> Result(Nil, AgentError)

Stop an Agent

pub fn update(
  agent: Agent,
  fun: fn(a) -> a,
) -> Result(Nil, AgentError)

Update the state synchronously

Search Document