Current section
Files
Jump to
Current section
Files
src/omnimessage@server.erl
-module(omnimessage@server).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([pipe/3, application/3, start_actor/2, subscribe/2, dispatch/1, shutdown/0, wisp_http_middleware/5, mist_websocket_pipe/4, mist_websocket_application/4]).
-export_type([encoder_decoder/3, app/3, composed_app/5]).
-type encoder_decoder(AARE, AARF, AARG) :: {encoder_decoder,
fun((AARE) -> {ok, AARF} | {error, nil}),
fun((AARF) -> {ok, AARE} | {error, AARG})}.
-opaque app(AARH, AARI, AARJ) :: {app,
fun((AARH) -> {AARI, lustre@effect:effect(AARJ)}),
fun((AARI, AARJ) -> {AARI, lustre@effect:effect(AARJ)}),
fun((AARI) -> lustre@internals@vdom:element(AARJ)),
gleam@option:option(gleam@dict:dict(binary(), fun((gleam@dynamic:dynamic_()) -> {ok,
AARJ} |
{error, list(gleam@dynamic:decode_error())})))}.
-opaque composed_app(AARK, AARL, AARM, AARN, AARO) :: {composed_app,
app(AARK, AARL, AARM),
encoder_decoder(AARM, AARN, AARO)}.
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 80).
-spec pipe(AARP, encoder_decoder(AARQ, AARP, AARR), fun((AARQ) -> AARQ)) -> {ok,
gleam@option:option(AARP)} |
{error, AARR}.
pipe(Msg, Encoder_decoder, Handler) ->
_pipe = Msg,
_pipe@1 = (erlang:element(3, Encoder_decoder))(_pipe),
_pipe@2 = gleam@result:map(_pipe@1, Handler),
_pipe@3 = gleam@result:map(_pipe@2, erlang:element(2, Encoder_decoder)),
gleam@result:map(_pipe@3, fun gleam@option:from_result/1).
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 123).
-spec application(
fun((AAWZ) -> {AAWS, lustre@effect:effect(AAWT)}),
fun((AAWS, AAWT) -> {AAWS, lustre@effect:effect(AAWT)}),
encoder_decoder(AAWT, AAWX, AAWY)
) -> composed_app(AAWZ, AAWS, AAWT, AAWX, AAWY).
application(Init, Update, Encoder_decoder) ->
View = fun(_) -> lustre@element:none() end,
{composed_app, {app, Init, Update, View, none}, Encoder_decoder}.
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 142).
-spec do_start_actor(app(AASR, any(), AAST), AASR) -> {ok,
gleam@erlang@process:subject(omnimessage@server@internal@lustre@runtime:action(AAST, lustre:server_component()))} |
{error, lustre:error()}.
do_start_actor(App, Flags) ->
On_attribute_change = gleam@option:unwrap(
erlang:element(5, App),
maps:new()
),
_pipe = (erlang:element(2, App))(Flags),
_pipe@1 = omnimessage@server@internal@lustre@runtime:start(
_pipe,
erlang:element(3, App),
erlang:element(4, App),
On_attribute_change
),
gleam@result:map_error(_pipe@1, fun(Field@0) -> {actor_error, Field@0} end).
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 134).
-spec start_actor(composed_app(AASC, any(), AASE, any(), any()), AASC) -> {ok,
gleam@erlang@process:subject(omnimessage@server@internal@lustre@runtime:action(AASE, lustre:server_component()))} |
{error, lustre:error()}.
start_actor(App, Flags) ->
do_start_actor(erlang:element(2, App), Flags).
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 155).
-spec subscribe(binary(), fun((AATC) -> nil)) -> omnimessage@server@internal@lustre@runtime:action(AATC, any()).
subscribe(Id, Dispatch) ->
{update_subscribe, Id, Dispatch}.
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 161).
-spec dispatch(AATE) -> omnimessage@server@internal@lustre@runtime:action(AATE, any()).
dispatch(Message) ->
{dispatch, Message}.
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 167).
-spec shutdown() -> omnimessage@server@internal@lustre@runtime:action(any(), any()).
shutdown() ->
shutdown.
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 180).
-spec wisp_http_middleware(
gleam@http@request:request(wisp@internal:connection()),
binary(),
encoder_decoder(AAYS, binary(), any()),
fun((AAYS) -> AAYS),
fun(() -> gleam@http@response:response(wisp:body()))
) -> gleam@http@response:response(wisp:body()).
wisp_http_middleware(Req, Path, Encoder_decoder, Handler, Fun) ->
case {erlang:element(8, Req) =:= Path, erlang:element(2, Req)} of
{true, post} ->
wisp:require_string_body(
Req,
fun(Req_body) ->
case begin
_pipe = Req_body,
pipe(_pipe, Encoder_decoder, Handler)
end of
{ok, {some, Res_body}} ->
_pipe@1 = wisp:response(200),
wisp:string_body(_pipe@1, Res_body);
{ok, none} ->
wisp:response(200);
{error, _} ->
wisp:unprocessable_entity()
end
end
);
{_, _} ->
Fun()
end.
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 215).
-spec mist_websocket_pipe(
gleam@http@request:request(mist@internal@http:connection()),
encoder_decoder(AATK, binary(), AATL),
fun((AATK) -> AATK),
fun((AATL) -> nil)
) -> gleam@http@response:response(mist:response_data()).
mist_websocket_pipe(Req, Encoder_decoder, Handler, On_error) ->
mist:websocket(Req, fun(Runtime, Conn, Msg) -> case Msg of
{text, Msg@1} ->
_ = case pipe(Msg@1, Encoder_decoder, Handler) of
{ok, {some, Encoded_msg}} ->
mist:send_text_frame(Conn, Encoded_msg);
{ok, none} ->
{ok, nil};
{error, Decode_error} ->
{ok, On_error(Decode_error)}
end,
gleam@otp@actor:continue(Runtime);
{binary, _} ->
gleam@otp@actor:continue(Runtime);
{custom, _} ->
gleam@otp@actor:continue(Runtime);
closed ->
{stop, normal};
shutdown ->
{stop, normal}
end end, fun(_) -> {none, none} end, fun(_) -> nil end).
-file("/Users/ofek/weedonandscott/Tech/omnimessage/omnimessage_server/src/omnimessage/server.gleam", 260).
-spec mist_websocket_application(
gleam@http@request:request(mist@internal@http:connection()),
composed_app(AATR, any(), any(), binary(), AATU),
AATR,
fun((AATU) -> nil)
) -> gleam@http@response:response(mist:response_data()).
mist_websocket_application(Req, App, Flags, On_error) ->
mist:websocket(Req, fun(Runtime, Conn, Msg) -> case Msg of
{text, Msg@1} ->
case (erlang:element(3, erlang:element(3, App)))(Msg@1) of
{ok, Decoded_msg} ->
gleam@erlang@process:send(
Runtime,
dispatch(Decoded_msg)
);
{error, Decode_error} ->
On_error(Decode_error)
end,
gleam@otp@actor:continue(Runtime);
{binary, _} ->
gleam@otp@actor:continue(Runtime);
{custom, Msg@2} ->
_assert_subject = case (erlang:element(
2,
erlang:element(3, App)
))(Msg@2) of
{ok, Msg@3} ->
mist:send_text_frame(Conn, Msg@3);
{error, _} ->
{ok, nil}
end,
{ok, _} = 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 => <<"omnimessage/server"/utf8>>,
function => <<"mist_websocket_application"/utf8>>,
line => 297})
end,
gleam@otp@actor:continue(Runtime);
closed ->
{stop, normal};
shutdown ->
{stop, normal}
end end, fun(_) ->
Self = gleam@erlang@process:new_subject(),
_assert_subject@1 = start_actor(App, Flags),
{ok, Runtime@1} = 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 => <<"omnimessage/server"/utf8>>,
function => <<"mist_websocket_application"/utf8>>,
line => 270})
end,
gleam@erlang@process:send(
Runtime@1,
subscribe(
<<"OMNIMESSAGE_AUTO_MIST"/utf8>>,
fun(_capture) ->
gleam@erlang@process:send(Self, _capture)
end
)
),
{Runtime@1,
{some,
begin
_pipe = gleam_erlang_ffi:new_selector(),
gleam@erlang@process:selecting(
_pipe,
Self,
fun gleam@function:identity/1
)
end}}
end, fun(Runtime@2) ->
gleam@erlang@process:send(Runtime@2, shutdown())
end).