Current section
Files
Jump to
Current section
Files
src/libero@rpc.erl
-module(libero@rpc).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/libero/rpc.gleam").
-export([update_from_server/2, send/4]).
-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(
" Client-side send machinery.\n"
"\n"
" `send` is the entry point used by libero-generated client stubs.\n"
" It takes the WebSocket URL, the module name, the typed MsgFromClient\n"
" message, and a callback to wrap the server's response into a\n"
" Lustre Msg.\n"
"\n"
" The JS FFI (rpc_ffi.mjs) opens the WebSocket lazily on first call\n"
" and caches the connection. Sends issued before the socket is open\n"
" are queued and flushed on the open event. Responses are matched\n"
" to sends in FIFO order.\n"
"\n"
" Developers don't usually call this module directly. They import\n"
" the per-module stubs the libero generator writes into their\n"
" client package, and those stubs delegate here.\n"
).
-file("src/libero/rpc.gleam", 53).
-spec ffi_register_push(binary(), fun((gleam@dynamic:dynamic_()) -> nil)) -> nil.
ffi_register_push(Module, Callback) ->
_ = Module,
_ = Callback,
erlang:error(#{gleam_error => panic,
message => <<"libero/rpc is a JavaScript-only module, unreachable on Erlang target"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"libero/rpc"/utf8>>,
function => <<"ffi_register_push"/utf8>>,
line => 59}).
-file("src/libero/rpc.gleam", 42).
?DOC(
" Handle server-initiated push messages on a module.\n"
" When the server pushes a MsgFromServer without a prior request,\n"
" the callback wraps it into a Lustre Msg for dispatch.\n"
).
-spec update_from_server(binary(), fun((gleam@dynamic:dynamic_()) -> XEL)) -> lustre@effect:effect(XEL).
update_from_server(Module, Handler) ->
lustre@effect:from(
fun(Dispatch) ->
ffi_register_push(Module, fun(Raw) -> Dispatch(Handler(Raw)) end)
end
).
-file("src/libero/rpc.gleam", 64).
-spec ffi_send(
binary(),
binary(),
any(),
fun((gleam@dynamic:dynamic_()) -> nil)
) -> nil.
ffi_send(Url, Module, Msg, On_response) ->
_ = Url,
_ = Module,
_ = Msg,
_ = On_response,
erlang:error(#{gleam_error => panic,
message => <<"libero/rpc is a JavaScript-only module, unreachable on Erlang target"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"libero/rpc"/utf8>>,
function => <<"ffi_send"/utf8>>,
line => 74}).
-file("src/libero/rpc.gleam", 26).
?DOC(
" Send a typed MsgFromClient message to the server via WebSocket and\n"
" deliver the server's response back to the Lustre update loop.\n"
"\n"
" The `on_response` callback wraps the decoded response (a Dynamic\n"
" value reconstructed from ETF) into a Lustre Msg so it can be\n"
" dispatched through `update`.\n"
).
-spec send(binary(), binary(), any(), fun((gleam@dynamic:dynamic_()) -> XEJ)) -> lustre@effect:effect(XEJ).
send(Url, Module, Msg, On_response) ->
lustre@effect:from(
fun(Dispatch) ->
ffi_send(
Url,
Module,
Msg,
fun(Raw) -> Dispatch(On_response(Raw)) end
)
end
).