Current section

Files

Jump to
shakespeare src shakespeare@actors@key_value.erl
Raw

src/shakespeare@actors@key_value.erl

-module(shakespeare@actors@key_value).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([set/3, get/2, start/0]).
-export_type([key_value_actor/1, message/1]).
-opaque key_value_actor(NRM) :: {key_value_actor,
gleam@erlang@process:subject(message(NRM))}.
-type message(NRN) :: {set, binary(), NRN} |
{get, binary(), gleam@erlang@process:subject({ok, NRN} | {error, nil})}.
-spec set(key_value_actor(NRS), binary(), NRS) -> nil.
set(Actor, Key, Value) ->
gleam@erlang@process:send(erlang:element(2, Actor), {set, Key, Value}).
-spec get(key_value_actor(NRV), binary()) -> {ok, NRV} | {error, nil}.
get(Actor, Key) ->
_pipe = gleam@erlang@process:try_call(
erlang:element(2, Actor),
fun(_capture) -> {get, Key, _capture} end,
10
),
_pipe@1 = gleam@result:map_error(_pipe, fun(_) -> nil end),
gleam@result:flatten(_pipe@1).
-spec handle_message(message(NRZ), gleam@dict:dict(binary(), NRZ)) -> gleam@otp@actor:next(message(NRZ), gleam@dict:dict(binary(), NRZ)).
handle_message(Message, State) ->
case Message of
{set, Key, Value} ->
gleam@otp@actor:continue(
begin
_pipe = State,
gleam@dict:insert(_pipe, Key, Value)
end
);
{get, Key@1, Client} ->
Value@1 = begin
_pipe@1 = State,
gleam@dict:get(_pipe@1, Key@1)
end,
gleam@erlang@process:send(Client, Value@1),
gleam@otp@actor:continue(State)
end.
-spec start() -> {ok, key_value_actor(any())} |
{error, gleam@otp@actor:start_error()}.
start() ->
_pipe = gleam@otp@actor:start(gleam@dict:new(), fun handle_message/2),
gleam@result:map(_pipe, fun(Field@0) -> {key_value_actor, Field@0} end).