-module(puddle). -compile([no_auto_import, nowarn_unused_vars]). -export([new/2, checkout/1, put_back/2]). -export_type([puddle/2, message/2]). -opaque puddle(GVS, GVT) :: {puddle, list({integer(), gleam@erlang@process:subject(message(GVS, GVT))}), list({integer(), gleam@erlang@process:subject(message(GVS, GVT))})}. -type message(GVU, GVV) :: shutdown | {utilize, fun((GVU) -> GVV), gleam@erlang@process:subject(GVV)}. -spec new(integer(), fun(() -> {ok, GVW} | {error, nil})) -> {ok, puddle(GVW, any())} | {error, nil}. new(Size, New_resource) -> case begin _pipe = gleam@list:repeat(<<""/utf8>>, Size), _pipe@1 = gleam@list:index_map(_pipe, fun(Index, _) -> Index end), gleam@list:try_map(_pipe@1, fun(Index@1) -> case New_resource() of {ok, Initial_state} -> _assert_subject = gleam@otp@actor:start( Initial_state, fun(Msg, Resource) -> case Msg of shutdown -> {stop, normal}; {utilize, Func, Client} -> Result = Func(Resource), gleam@otp@actor:send(Client, Result), gleam@otp@actor:continue(Resource) end end ), {ok, Subject} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"puddle"/utf8>>, function => <<"new"/utf8>>, line => 27}) end, _pipe@2 = {Index@1, Subject}, {ok, _pipe@2}; {error, nil} -> {error, nil} end end) end of {ok, Idle} -> _pipe@3 = {puddle, Idle, []}, {ok, _pipe@3}; {error, nil} -> {error, nil} end. -spec checkout(puddle(GYV, GYW)) -> {ok, {{integer(), gleam@erlang@process:subject(message(GYV, GYW))}, puddle(GYV, GYW)}} | {error, nil}. checkout(Puddle) -> case Puddle of {puddle, Idle, Busy} -> case Idle of [{Id, Subject} | Rest] -> {ok, {{Id, Subject}, {puddle, Rest, gleam@list:prepend(Busy, {Id, Subject})}}}; [] -> {error, nil} end end. -spec put_back(puddle(GZG, GZH), integer()) -> puddle(GZG, GZH). put_back(Puddle, Subject_id) -> case Puddle of {puddle, Idle, Busy} -> _assert_subject = gleam@list:key_pop(Busy, Subject_id), {ok, {Subject, Busy@1}} = case _assert_subject of {ok, {_, _}} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"puddle"/utf8>>, function => <<"put_back"/utf8>>, line => 72}) end, {puddle, gleam@list:key_set(Idle, Subject_id, Subject), Busy@1} end.