Current section
Files
Jump to
Current section
Files
src/chrobot@internal@utils.erl
-module(chrobot@internal@utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([add_optional/3, alert_encode_dynamic/1, try_call_with_subject/4]).
-spec add_optional(
list({binary(), gleam@json:json()}),
gleam@option:option(OEC),
fun((OEC) -> {binary(), gleam@json:json()})
) -> list({binary(), gleam@json:json()}).
add_optional(Prop_encoders, Value, Callback) ->
case Value of
{some, A} ->
[Callback(A) | Prop_encoders];
none ->
Prop_encoders
end.
-spec alert_encode_dynamic(any()) -> gleam@json:json().
alert_encode_dynamic(Input_value) ->
gleam@io:println(
<<"\x{1b}[31mWARNING: You passed a dymamic value to a protocol encoder!
Dynamic values cannot be encoded, the value will be set to null instead.
\x{1b}[0m"/utf8>>
),
gleam@io:println(
<<"The value was: "/utf8, (gleam@string:inspect(Input_value))/binary>>
),
gleam@json:null().
-spec try_call_with_subject(
gleam@erlang@process:subject(OEH),
fun((gleam@erlang@process:subject(OEJ)) -> OEH),
gleam@erlang@process:subject(OEJ),
integer()
) -> {ok, OEJ} | {error, gleam@erlang@process:call_error(OEJ)}.
try_call_with_subject(Subject, Make_request, Reply_subject, Timeout) ->
Monitor = gleam@erlang@process:monitor_process(
gleam@erlang@process:subject_owner(Subject)
),
gleam@erlang@process:send(Subject, Make_request(Reply_subject)),
Result = begin
_pipe = gleam_erlang_ffi:new_selector(),
_pipe@1 = gleam@erlang@process:selecting(
_pipe,
Reply_subject,
fun(Field@0) -> {ok, Field@0} end
),
_pipe@2 = gleam@erlang@process:selecting_process_down(
_pipe@1,
Monitor,
fun(Down) -> {error, {callee_down, erlang:element(3, Down)}} end
),
gleam_erlang_ffi:select(_pipe@2, Timeout)
end,
gleam_erlang_ffi:demonitor(Monitor),
case Result of
{error, nil} ->
{error, call_timeout};
{ok, Res} ->
Res
end.