Current section
Files
Jump to
Current section
Files
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, GXE} | {error, GXF}),
fun((binary()) -> GXF),
fun(({integer(), {ok, GXE} | {error, GXF}}) -> 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() ->
gleam@erlang:system_time(millisecond).
-spec task_map(list(GXM), fun((GXM) -> GXO)) -> list({GXM,
gleam@otp@task:task(GXO)}).
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({GXR, gleam@otp@task:task(GXS)}), timing()) -> list({GXR,
{ok,
GXS} |
{error,
binary()}}).
try_await_many(Tasks, Timing) ->
_pipe@4 = case Timing of
endless ->
fun(_capture) ->
gleam@pair:map_second(
_capture,
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,
fun(_capture@1) ->
gleam@pair:map_second(
_capture@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,
gleam@list:map(Tasks, _pipe@4).
-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} ->
gleam@string:append(
<<"task exited for some reason: "/utf8>>,
gleam@string:inspect(S)
)
end.