-module(internal@erl_impl). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/internal/erl_impl.gleam"). -export([init/0, with_timeout/2, get/1, set/2, update/2, update_and_retrieve/2, retrieve_and_update/2, new/2]). -export_type([msg/1, state/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -type msg(FLS) :: {replace, FLS} | {update, fun((FLS) -> FLS)} | {update_then_retrieve, gleam@erlang@process:subject(FLS), fun((FLS) -> FLS)} | {retrieve_then_update, gleam@erlang@process:subject(FLS), fun((FLS) -> FLS)}. -opaque state(FLT) :: {state, gleam@erlang@process:subject(msg(FLT)), smut@types:value_handle(FLT), integer()}. -file("src/internal/erl_impl.gleam", 14). ?DOC(false). -spec from_start_error(gleam@otp@actor:start_error()) -> smut@types:smut_error(). from_start_error(Error) -> case Error of init_timeout -> {new_failed, <<"timeout"/utf8>>}; {init_failed, Reason} -> {new_failed, Reason}; {init_exited, Reason@1} -> case Reason@1 of normal -> {new_exited, <<"process exited normally"/utf8>>}; killed -> {new_exited, <<"process was killed"/utf8>>}; {abnormal, Reason@2} -> _pipe = gleam@string:inspect(Reason@2), {new_exited, _pipe} end end. -file("src/internal/erl_impl.gleam", 51). ?DOC(false). -spec init() -> smut@types:table_handle(). init() -> erl_shim:get_table(). -file("src/internal/erl_impl.gleam", 69). ?DOC(false). -spec with_timeout(state(FLZ), integer()) -> state(FLZ). with_timeout(State, Ms) -> {state, erlang:element(2, State), erlang:element(3, State), Ms}. -file("src/internal/erl_impl.gleam", 73). ?DOC(false). -spec lookup(smut@types:value_handle(FMC)) -> {ok, FMC} | {error, nil}. lookup(Handle) -> {value_handle, Table, Key} = Handle, erl_shim:lookup(Table, Key). -file("src/internal/erl_impl.gleam", 78). ?DOC(false). -spec replace(smut@types:value_handle(FMG), FMG) -> nil. replace(Handle, New_val) -> {value_handle, Table, Key} = Handle, erl_shim:replace(Table, Key, New_val). -file("src/internal/erl_impl.gleam", 83). ?DOC(false). -spec insert_new(smut@types:table_handle(), FMI) -> smut@types:value_handle(FMI). insert_new(Table, Val) -> Key = erl_shim:insert_new(Table, Val), {value_handle, Table, Key}. -file("src/internal/erl_impl.gleam", 88). ?DOC(false). -spec run_actor(smut@types:value_handle(FMK), msg(FMK)) -> gleam@otp@actor:next(smut@types:value_handle(FMK), msg(FMK)). run_actor(Handle, Message) -> case Message of {replace, New_val} -> replace(Handle, New_val), gleam@otp@actor:continue(Handle); {update, F} -> case lookup(Handle) of {ok, Val} -> _pipe = F(Val), replace(Handle, _pipe), gleam@otp@actor:continue(Handle); {error, _} -> gleam@otp@actor:continue(Handle) end; {update_then_retrieve, Return, F@1} -> case lookup(Handle) of {ok, Val@1} -> New_val@1 = F@1(Val@1), replace(Handle, New_val@1), gleam@erlang@process:send(Return, New_val@1), gleam@otp@actor:continue(Handle); {error, _} -> gleam@otp@actor:continue(Handle) end; {retrieve_then_update, Return@1, F@2} -> case lookup(Handle) of {ok, Val@2} -> gleam@erlang@process:send(Return@1, Val@2), _pipe@1 = F@2(Val@2), replace(Handle, _pipe@1), gleam@otp@actor:continue(Handle); {error, _} -> gleam@otp@actor:continue(Handle) end end. -file("src/internal/erl_impl.gleam", 145). ?DOC(false). -spec get(state(FMV)) -> {ok, FMV} | {error, nil}. get(State) -> lookup(erlang:element(3, State)). -file("src/internal/erl_impl.gleam", 166). ?DOC(false). -spec set(FMZ, state(FMZ)) -> nil. set(New_val, State) -> gleam@erlang@process:send(erlang:element(2, State), {replace, New_val}). -file("src/internal/erl_impl.gleam", 174). ?DOC(false). -spec update(fun((FNB) -> FNB), state(FNB)) -> nil. update(With, State) -> gleam@erlang@process:send(erlang:element(2, State), {update, With}). -file("src/internal/erl_impl.gleam", 178). ?DOC(false). -spec update_and_retrieve(fun((FND) -> FND), state(FND)) -> {ok, FND} | {error, nil}. update_and_retrieve(With, State) -> Return = gleam@erlang@process:new_subject(), gleam@erlang@process:send( erlang:element(2, State), {update_then_retrieve, Return, With} ), gleam@erlang@process:'receive'(Return, erlang:element(4, State)). -file("src/internal/erl_impl.gleam", 187). ?DOC(false). -spec retrieve_and_update(fun((FNH) -> FNH), state(FNH)) -> {ok, FNH} | {error, nil}. retrieve_and_update(With, State) -> Return = gleam@erlang@process:new_subject(), gleam@erlang@process:send( erlang:element(2, State), {retrieve_then_update, Return, With} ), gleam@erlang@process:'receive'(Return, erlang:element(4, State)). -file("src/internal/erl_impl.gleam", 130). ?DOC(false). -spec new(smut@types:table_handle(), FMR) -> {ok, state(FMR)} | {error, smut@types:smut_error()}. new(Table, Initial_val) -> Value_handle = insert_new(Table, Initial_val), gleam@result:'try'( begin _pipe = gleam@otp@actor:new(Value_handle), _pipe@1 = gleam@otp@actor:on_message(_pipe, fun run_actor/2), _pipe@2 = gleam@otp@actor:start(_pipe@1), gleam@result:map_error(_pipe@2, fun from_start_error/1) end, fun(Actor) -> _pipe@3 = {state, erlang:element(3, Actor), Value_handle, 5000}, {ok, _pipe@3} end ).