Current section

Files

Jump to
gladvent src cmd.erl
Raw

src/cmd.erl

-module(cmd).
-compile(no_auto_import).
-export([exec/5]).
-export_type([timing/0]).
-type timing() :: endless | {ending, integer()}.
-spec exec(
list(integer()),
timing(),
fun((integer()) -> {ok, GYI} | {error, GYJ}),
fun((binary()) -> GYJ),
fun(({integer(), {ok, GYI} | {error, GYJ}}) -> binary())
) -> list(binary()).
exec(Days, Timing, Do, Other, Collect) ->
_pipe = Days,
_pipe@1 = task_map(_pipe, Do),
_pipe@2 = try_await_many(_pipe@1, Timing),
_pipe@3 = gleam@iterator:from_list(_pipe@2),
_pipe@6 = gleam@iterator:map(
_pipe@3,
fun(X) ->
_pipe@4 = X,
_pipe@5 = gleam@pair:map_second(
_pipe@4,
fun(_capture) -> gleam@result:map_error(_capture, Other) end
),
gleam@pair:map_second(_pipe@5, fun gleam@result:flatten/1)
end
),
_pipe@7 = gleam@iterator:map(_pipe@6, Collect),
gleam@iterator:to_list(_pipe@7).
-spec now_ms() -> integer().
now_ms() ->
os:system_time(millisecond).
-spec task_map(list(GYQ), fun((GYQ) -> GYS)) -> list({GYQ,
gleam@otp@task:task(GYS)}).
task_map(L, F) ->
gleam@list:map(
L,
fun(X) ->
{X, gleam@otp@task:async(fun() -> F(X) end)}
end
).
-spec try_await_many(list({GYV, gleam@otp@task:task(GYW)}), timing()) -> list({GYV,
{ok, GYW} | {error, binary()}}).
try_await_many(Tasks, Timing) ->
case Timing of
endless ->
gleam@list:map(
Tasks,
fun(Tup) ->
gleam@pair:map_second(
Tup,
fun(T) ->
_pipe = gleam@otp@task:try_await_forever(T),
gleam@result:map_error(
_pipe,
fun await_err_to_string/1
)
end
)
end
);
{ending, Timeout} ->
End = now_ms() + Timeout,
gleam@list:map(
Tasks,
fun(Tup@1) ->
gleam@pair:map_second(
Tup@1,
fun(T@1) ->
_pipe@1 = End - now_ms(),
_pipe@2 = gleam@int:clamp(_pipe@1, 0, Timeout),
_pipe@3 = gleam@otp@task:try_await(T@1, _pipe@2),
gleam@result:map_error(
_pipe@3,
fun await_err_to_string/1
)
end
)
end
)
end.
-spec await_err_to_string(gleam@otp@task:await_error()) -> binary().
await_err_to_string(Err) ->
case Err of
timeout ->
<<"task timed out"/utf8>>;
{exit, S} ->
<<"task exited for some reason: "/utf8,
(gleam@string:inspect(S))/binary>>
end.