Files
CljElixir/stubs/Agent.clj
2026-03-09 23:09:46 -04:00

61 lines
1.7 KiB
Clojure

(ns Agent
"Elixir Agent module — simple state wrapper around a process.
In CljElixir: (Agent/start-link (fn [] initial-state)), etc.
Agents are a simpler alternative to GenServer for pure state management.")
(defn start
"Starts an Agent without linking. Returns {:ok pid}.
(Agent/start (fn [] {:count 0}))"
([fun])
([fun opts])
([module fun args])
([module fun args opts]))
(defn start-link
"Starts an Agent linked to the current process. Returns {:ok pid}.
(Agent/start-link (fn [] {:count 0}))
(Agent/start-link (fn [] {:count 0}) :name :my-agent)"
([fun])
([fun opts])
([module fun args])
([module fun args opts]))
(defn get
"Gets the agent state by applying `fun`. Blocks until result.
(Agent/get pid (fn [state] (:count state))) ;=> 0
(Agent/get pid (fn [state] state) 5000) ;=> with 5s timeout"
([agent fun])
([agent fun timeout])
([agent module fun args])
([agent module fun args timeout]))
(defn update
"Updates the agent state by applying `fun`. Returns :ok.
(Agent/update pid (fn [state] (update state :count inc))) ;=> :ok"
([agent fun])
([agent fun timeout])
([agent module fun args])
([agent module fun args timeout]))
(defn get-and-update
"Gets and updates in one operation. `fun` returns {get_value, new_state}.
(Agent/get-and-update pid (fn [state] {(:count state) (update state :count inc)}))"
([agent fun])
([agent fun timeout])
([agent module fun args])
([agent module fun args timeout]))
(defn cast
"Async update. Returns :ok immediately.
(Agent/cast pid (fn [state] (assoc state :key \"val\"))) ;=> :ok"
([agent fun])
([agent module fun args]))
(defn stop
"Stops the agent.
(Agent/stop pid) ;=> :ok"
([agent])
([agent reason])
([agent reason timeout]))