Current section

Files

Jump to
glixir src glixir@agent.erl
Raw

src/glixir@agent.erl

-module(glixir@agent).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/glixir/agent.gleam").
-export([start/1, start_named/2, get/3, get_timeout/4, update/2, cast/2, get_and_update/3, stop/1, pid/1]).
-export_type([agent/0, agent_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-opaque agent() :: {agent, gleam@erlang@process:pid_()}.
-type agent_error() :: {start_error, binary()} |
timeout |
agent_down |
{decode_error, binary()}.
-file("src/glixir/agent.gleam", 50).
?DOC(" Start a new Agent with initial state\n").
-spec start(fun(() -> any())) -> {ok, agent()} | {error, agent_error()}.
start(Initial_fun) ->
case 'Elixir.Agent':start_link(Initial_fun, []) of
{ok, Pid} ->
{ok, {agent, Pid}};
{error, Reason} ->
{error, {start_error, gleam@string:inspect(Reason)}}
end.
-file("src/glixir/agent.gleam", 58).
?DOC(" Start an Agent with a name\n").
-spec start_named(binary(), fun(() -> any())) -> {ok, agent()} |
{error, agent_error()}.
start_named(Name, Initial_fun) ->
Name_atom = erlang:binary_to_atom(Name),
Options = [erlang:list_to_tuple(
[gleam_erlang_ffi:identity(erlang:binary_to_atom(<<"name"/utf8>>)),
gleam_erlang_ffi:identity(Name_atom)]
)],
case 'Elixir.Agent':start_link(Initial_fun, Options) of
{ok, Pid} ->
{ok, {agent, Pid}};
{error, Reason} ->
{error, {start_error, gleam@string:inspect(Reason)}}
end.
-file("src/glixir/agent.gleam", 77).
?DOC(
" Get the current state\n"
" Requires a decoder for the expected type `b`.\n"
).
-spec get(agent(), fun((any()) -> EGE), gleam@dynamic@decode:decoder(EGE)) -> {ok,
EGE} |
{error, agent_error()}.
get(Agent, Fun, Decoder) ->
{agent, Pid} = Agent,
Result_dynamic = 'Elixir.Agent':get(Pid, Fun, 5000),
_pipe = gleam@dynamic@decode:run(Result_dynamic, Decoder),
_pipe@1 = gleam@result:map_error(_pipe, fun gleam@string:inspect/1),
gleam@result:map_error(_pipe@1, fun(Field@0) -> {decode_error, Field@0} end).
-file("src/glixir/agent.gleam", 93).
?DOC(
" Get with custom timeout\n"
" Requires a decoder for the expected type `b`.\n"
).
-spec get_timeout(
agent(),
fun((any()) -> EGJ),
integer(),
gleam@dynamic@decode:decoder(EGJ)
) -> {ok, EGJ} | {error, agent_error()}.
get_timeout(Agent, Fun, Timeout, Decoder) ->
{agent, Pid} = Agent,
Result_dynamic = 'Elixir.Agent':get(Pid, Fun, Timeout),
_pipe = gleam@dynamic@decode:run(Result_dynamic, Decoder),
_pipe@1 = gleam@result:map_error(_pipe, fun gleam@string:inspect/1),
gleam@result:map_error(_pipe@1, fun(Field@0) -> {decode_error, Field@0} end).
-file("src/glixir/agent.gleam", 109).
?DOC(" Update the state synchronously\n").
-spec update(agent(), fun((EGN) -> EGN)) -> {ok, nil} | {error, agent_error()}.
update(Agent, Fun) ->
{agent, Pid} = Agent,
case erlang:atom_to_binary('Elixir.Agent':update(Pid, Fun, 5000)) of
<<"ok"/utf8>> ->
{ok, nil};
_ ->
{error, agent_down}
end.
-file("src/glixir/agent.gleam", 119).
?DOC(" Update the state asynchronously\n").
-spec cast(agent(), fun((EGQ) -> EGQ)) -> nil.
cast(Agent, Fun) ->
{agent, Pid} = Agent,
_ = 'Elixir.Agent':cast(Pid, Fun),
nil.
-file("src/glixir/agent.gleam", 127).
?DOC(
" Get and update in one operation\n"
" Requires a decoder for the expected type `b`.\n"
).
-spec get_and_update(
agent(),
fun((EGR) -> {EGS, EGR}),
gleam@dynamic@decode:decoder(EGS)
) -> {ok, EGS} | {error, agent_error()}.
get_and_update(Agent, Fun, Decoder) ->
{agent, Pid} = Agent,
Result_dynamic = 'Elixir.Agent':get_and_update(Pid, Fun, 5000),
_pipe = gleam@dynamic@decode:run(Result_dynamic, Decoder),
_pipe@1 = gleam@result:map_error(_pipe, fun gleam@string:inspect/1),
gleam@result:map_error(_pipe@1, fun(Field@0) -> {decode_error, Field@0} end).
-file("src/glixir/agent.gleam", 142).
?DOC(" Stop an Agent\n").
-spec stop(agent()) -> {ok, nil} | {error, agent_error()}.
stop(Agent) ->
{agent, Pid} = Agent,
Normal = erlang:binary_to_atom(<<"normal"/utf8>>),
case erlang:atom_to_binary('Elixir.Agent':stop(Pid, Normal, 5000)) of
<<"ok"/utf8>> ->
{ok, nil};
_ ->
{error, agent_down}
end.
-file("src/glixir/agent.gleam", 153).
?DOC(" Get the PID of an Agent\n").
-spec pid(agent()) -> gleam@erlang@process:pid_().
pid(Agent) ->
{agent, P} = Agent,
P.