Current section
Files
Jump to
Current section
Files
src/spectator@internal@common.erl
-module(spectator@internal@common).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/spectator/internal/common.gleam").
-export([encode_params/1, add_param/3, get_param/2, sanitize_params/1, truncate_float/1, format_percentage/1, emit_after/4, bool_compare/2, get_refresh_interval/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).
-file("src/spectator/internal/common.gleam", 29).
?DOC(false).
-spec encode_params(list({binary(), binary()})) -> binary().
encode_params(Params) ->
case Params of
[] ->
<<""/utf8>>;
_ ->
<<"?"/utf8, (gleam@uri:query_to_string(Params))/binary>>
end.
-file("src/spectator/internal/common.gleam", 36).
?DOC(false).
-spec add_param(list({binary(), binary()}), binary(), binary()) -> list({binary(),
binary()}).
add_param(Params, Key, Value) ->
case Value of
<<""/utf8>> ->
Params;
_ ->
[{Key, Value} | Params]
end.
-file("src/spectator/internal/common.gleam", 43).
?DOC(false).
-spec get_param(list({binary(), binary()}), binary()) -> {ok, binary()} |
{error, nil}.
get_param(Params, Key) ->
gleam@list:find_map(Params, fun(P) -> case P of
{K, <<""/utf8>>} when K =:= Key ->
{error, nil};
{K@1, V} when K@1 =:= Key ->
{ok, V};
_ ->
{error, nil}
end end).
-file("src/spectator/internal/common.gleam", 53).
?DOC(false).
-spec sanitize_params(list({binary(), binary()})) -> list({binary(), binary()}).
sanitize_params(Params) ->
gleam@list:filter_map(Params, fun(P) -> case P of
{<<"node"/utf8>>, _} ->
{ok, P};
{<<"refresh"/utf8>>, _} ->
{ok, P};
_ ->
{error, nil}
end end).
-file("src/spectator/internal/common.gleam", 75).
?DOC(false).
-spec truncate_float(float()) -> binary().
truncate_float(Value) ->
spectator_ffi:truncate_float(Value).
-file("src/spectator/internal/common.gleam", 77).
?DOC(false).
-spec format_percentage(float()) -> binary().
format_percentage(Value) ->
<<(spectator_ffi:truncate_float(Value))/binary, "%"/utf8>>.
-file("src/spectator/internal/common.gleam", 81).
?DOC(false).
-spec emit_after(
integer(),
ACKB,
gleam@option:option(gleam@erlang@process:subject(ACKB)),
fun((gleam@erlang@process:subject(ACKB)) -> ACKB)
) -> lustre@effect:effect(ACKB).
emit_after(Delay, Msg, Subject, Subject_created_message) ->
case Subject of
{some, Self} ->
lustre@effect:from(
fun(_) ->
_ = gleam@erlang@process:send_after(Self, Delay, Msg),
nil
end
);
none ->
lustre@server_component:select(
fun(Dispatch, Subject@1) ->
Selector = begin
_pipe = gleam_erlang_ffi:new_selector(),
gleam@erlang@process:select(_pipe, Subject@1)
end,
_ = gleam@erlang@process:send_after(Subject@1, Delay, Msg),
Dispatch(Subject_created_message(Subject@1)),
Selector
end
)
end.
-file("src/spectator/internal/common.gleam", 105).
?DOC(false).
-spec bool_compare(boolean(), boolean()) -> gleam@order:order().
bool_compare(A, B) ->
case {A, B} of
{true, true} ->
eq;
{true, false} ->
gt;
{false, false} ->
eq;
{false, true} ->
lt
end.
-file("src/spectator/internal/common.gleam", 63).
?DOC(false).
-spec get_refresh_interval(list({binary(), binary()})) -> integer().
get_refresh_interval(Params) ->
case get_param(Params, <<"refresh"/utf8>>) of
{ok, Rate} ->
case gleam_stdlib:parse_int(Rate) of
{ok, R} ->
R;
{error, _} ->
1000
end;
{error, _} ->
1000
end.