genserver
Values
pub fn agent_get(agent: types.Agent, get_fun: fn(a) -> b) -> b
Get the current state from an Agent
Example
let value = agent_get(agent, fn(state) { state })
pub fn agent_pid(agent: types.Agent) -> process.Pid
Get the underlying PID from an Agent
Example
let agent_pid = agent_pid(agent)
pub fn agent_start(
initial_fun: fn() -> a,
) -> Result(types.Agent, types.GenServerError)
Start an Agent with an initial value function
Agents are simple wrappers around GenServers for managing state.
Example
let assert Ok(agent) = agent_start(fn() { 0 })
pub fn agent_stop(
agent: types.Agent,
) -> Result(Nil, types.GenServerError)
Stop an Agent
Example
let assert Ok(_) = agent_stop(agent)
pub fn agent_update(
agent: types.Agent,
update_fun: fn(a) -> a,
) -> Result(Nil, types.GenServerError)
Update the state of an Agent
Example
let assert Ok(_) = agent_update(agent, fn(state) { state + 1 })
pub fn atom(name: String) -> atom.Atom
Create an atom from a string
Useful for creating atoms to use in messages or function calls.
Example
let ok_atom = atom("ok")
pub fn call(
server: types.GenServer,
request: a,
) -> Result(b, types.GenServerError)
Send a synchronous call to the GenServer with default 5s timeout
This is equivalent to gen_server:call/2
in Erlang/Elixir.
The GenServer’s handle_call/3
callback will be invoked.
Example
let assert Ok(response) = call(server, "get_state")
pub fn call_timeout(
server: types.GenServer,
request: a,
timeout: Int,
) -> Result(b, types.GenServerError)
Send a synchronous call to the GenServer with custom timeout
Example
let assert Ok(response) = call_timeout(server, "slow_operation", 10000)
pub fn cast(
server: types.GenServer,
request: a,
) -> Result(Nil, types.GenServerError)
Send an asynchronous cast to the GenServer
This is equivalent to gen_server:cast/2
in Erlang/Elixir.
The GenServer’s handle_cast/2
callback will be invoked.
Example
let assert Ok(_) = cast(server, #("update_state", "new_value"))
pub fn pid(server: types.GenServer) -> process.Pid
Get the underlying PID from a GenServer
Use this when you need the raw PID for advanced operations.
Example
let server_pid = pid(server)
pub fn send_message(server: types.GenServer, message: a) -> Nil
Send a raw message to the GenServer (bypasses GenServer protocol)
This sends a message directly to the process, which will be handled
by the GenServer’s handle_info/2
callback. Useful for custom protocols.
Example
let my_pid = process.self()
let message = tagged_message("custom_event", my_pid, "some_data")
send_message(server, message)
pub fn start(
module: String,
args: a,
) -> Result(types.GenServer, types.GenServerError)
Start a GenServer using Module.start/1 (returns PID directly)
This calls the module’s start/1 function directly, which should return a PID. Use this when your Elixir GenServer has a custom start function.
Example
let assert Ok(server) = start("MyServer", "initial_state")
pub fn start_link(
module: String,
args: a,
) -> Result(types.GenServer, types.GenServerError)
Start a GenServer using Module.start_link/1
This follows the OTP convention and will start the GenServer under a supervisor if called from a supervision tree.
Example
let assert Ok(server) = start_link("MyServer", "initial_state")
pub fn tagged_message(
tag: String,
from: process.Pid,
content: a,
) -> #(atom.Atom, process.Pid, a)
Create a tagged message tuple for handle_info callbacks
This creates a tuple in the format {tag, from_pid, content}
which
is a common pattern for custom messages in Elixir GenServers.
Example
let message = tagged_message("gleam_msg", process.self(), "Hello!")
// Creates: {:gleam_msg, #PID<...>, "Hello!"}