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, get_param/2, truncate_float/1, format_percentage/1, static_file/1, emit_after/4]).
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 30).
-spec encode_params(list({binary(), binary()})) -> binary().
encode_params(Params) ->
Pair = fun(T) ->
gleam@string_builder:from_strings(
[gleam@uri:percent_encode(erlang:element(1, T)),
<<"="/utf8>>,
gleam@uri:percent_encode(erlang:element(2, T))]
)
end,
case Params of
[] ->
<<""/utf8>>;
_ ->
_pipe = Params,
_pipe@1 = gleam@list:map(_pipe, Pair),
_pipe@2 = gleam@list:intersperse(
_pipe@1,
gleam@string_builder:from_string(<<"&"/utf8>>)
),
_pipe@3 = gleam@string_builder:concat(_pipe@2),
_pipe@4 = gleam@string_builder:prepend(_pipe@3, <<"?"/utf8>>),
gleam@string_builder:to_string(_pipe@4)
end.
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 50).
-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, V} when K =:= Key ->
gleam@uri:percent_decode(V);
_ ->
{error, nil}
end end).
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 60).
-spec truncate_float(float()) -> binary().
truncate_float(Value) ->
spectator_ffi:truncate_float(Value).
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 62).
-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", 66).
-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 => 67})
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 => 68})
end,
Data.
-file("/Users/jonas/Projects/spectator/src/spectator/internal/common.gleam", 72).
-spec emit_after(
integer(),
APTW,
gleam@option:option(gleam@erlang@process:subject(APTW)),
fun((gleam@erlang@process:subject(APTW)) -> APTW)
) -> lustre@effect:effect(APTW).
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.