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]).
-export([encode_params/1, add_param/3, get_param/2, sanitize_params/1, truncate_float/1, format_percentage/1, static_file/1, emit_after/4, bool_compare/2, get_refresh_interval/1]).
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 31).
-spec encode_params(list({binary(), binary()})) -> binary().
encode_params(Params) ->
case Params of
[] ->
<<""/utf8>>;
_ ->
<<"?"/utf8, (gleam@uri:query_to_string(Params))/binary>>
end.
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 38).
-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("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 45).
-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("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 55).
-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};
{<<"cookie"/utf8>>, _} ->
{ok, P};
{<<"refresh"/utf8>>, _} ->
{ok, P};
_ ->
{error, nil}
end end).
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 78).
-spec truncate_float(float()) -> binary().
truncate_float(Value) ->
spectator_ffi:truncate_float(Value).
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 80).
-spec format_percentage(float()) -> binary().
format_percentage(Value) ->
<<(spectator_ffi:truncate_float(Value))/binary, "%"/utf8>>.
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 84).
-spec static_file(binary()) -> binary().
static_file(Name) ->
_assert_subject = gleam_erlang_ffi:priv_directory(<<"spectator"/utf8>>),
{ok, Priv} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"spectator/internal/common"/utf8>>,
function => <<"static_file"/utf8>>,
line => 85})
end,
_assert_subject@1 = simplifile:read(
<<<<Priv/binary, "/"/utf8>>/binary, Name/binary>>
),
{ok, Data} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"spectator/internal/common"/utf8>>,
function => <<"static_file"/utf8>>,
line => 86})
end,
Data.
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 90).
-spec emit_after(
integer(),
ARBX,
gleam@option:option(gleam@erlang@process:subject(ARBX)),
fun((gleam@erlang@process:subject(ARBX)) -> ARBX)
) -> lustre@effect:effect(ARBX).
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:selecting(
_pipe,
Subject@1,
fun(Msg@1) -> Msg@1 end
)
end,
_ = gleam@erlang@process:send_after(Subject@1, Delay, Msg),
Dispatch(Subject_created_message(Subject@1)),
Selector
end
)
end.
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 113).
-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("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 66).
-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.