Packages
Typed distributed messaging for Gleam on the BEAM.
Retired package: Deprecated - The project needs to be redesigned around a much smaller and clearer core.
Current section
Files
Jump to
Current section
Files
src/distribute@global.erl
-module(distribute@global).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/distribute/global.gleam").
-export([new/2, from_pid/3, subject/1, owner/1, encoder/1, decoder/1, send/2, 'receive'/2]).
-export_type([global_subject/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-opaque global_subject(HXK) :: {global_subject,
gleam@erlang@process:subject(bitstring()),
fun((HXK) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}),
fun((bitstring()) -> {ok, HXK} |
{error, distribute@codec:decode_error()})}.
-file("src/distribute/global.gleam", 23).
?DOC(
" Create a new global subject for the current process with required codec.\n"
" The encoder and decoder ensure all messages are type-safe.\n"
).
-spec new(
fun((HXL) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}),
fun((bitstring()) -> {ok, HXL} | {error, distribute@codec:decode_error()})
) -> global_subject(HXL).
new(Encoder, Decoder) ->
Subject = gleam@erlang@process:unsafely_create_subject(
erlang:self(),
gleam@dynamic:nil()
),
{global_subject, Subject, Encoder, Decoder}.
-file("src/distribute/global.gleam", 33).
?DOC(
" Create a global subject from a Pid with required codec.\n"
" Useful when you have a Pid from registry lookup.\n"
).
-spec from_pid(
gleam@erlang@process:pid_(),
fun((HXP) -> {ok, bitstring()} | {error, distribute@codec:encode_error()}),
fun((bitstring()) -> {ok, HXP} | {error, distribute@codec:decode_error()})
) -> global_subject(HXP).
from_pid(Pid, Encoder, Decoder) ->
Subject = gleam@erlang@process:unsafely_create_subject(
Pid,
gleam@dynamic:nil()
),
{global_subject, Subject, Encoder, Decoder}.
-file("src/distribute/global.gleam", 44).
?DOC(
" Get the underlying Subject(BitArray) for low-level operations.\n"
" Prefer using `send` and `receive` methods for type-safe messaging.\n"
).
-spec subject(global_subject(any())) -> gleam@erlang@process:subject(bitstring()).
subject(Global) ->
erlang:element(2, Global).
-file("src/distribute/global.gleam", 49).
?DOC(" Get the owner Pid of this global subject.\n").
-spec owner(global_subject(any())) -> {ok, gleam@erlang@process:pid_()} |
{error, nil}.
owner(Global) ->
gleam@erlang@process:subject_owner(erlang:element(2, Global)).
-file("src/distribute/global.gleam", 54).
?DOC(" Get the encoder for this global subject.\n").
-spec encoder(global_subject(HYA)) -> fun((HYA) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
encoder(Global) ->
erlang:element(3, Global).
-file("src/distribute/global.gleam", 59).
?DOC(" Get the decoder for this global subject.\n").
-spec decoder(global_subject(HYD)) -> fun((bitstring()) -> {ok, HYD} |
{error, distribute@codec:decode_error()}).
decoder(Global) ->
erlang:element(4, Global).
-file("src/distribute/global.gleam", 65).
?DOC(
" Send a type-safe message through this global subject.\n"
" The message is automatically encoded using the subject's encoder.\n"
).
-spec send(global_subject(HYG), HYG) -> {ok, nil} |
{error, distribute@codec:encode_error()}.
send(Global, Message) ->
case distribute@codec:encode(erlang:element(3, Global), Message) of
{ok, Binary} ->
gleam@erlang@process:send(erlang:element(2, Global), Binary),
{ok, nil};
{error, Err} ->
{error, Err}
end.
-file("src/distribute/global.gleam", 80).
?DOC(
" Receive a type-safe message from this global subject.\n"
" The message is automatically decoded using the subject's decoder.\n"
).
-spec 'receive'(global_subject(HYK), integer()) -> {ok, HYK} |
{error, distribute@codec:decode_error()}.
'receive'(Global, Timeout_ms) ->
case gleam@erlang@process:'receive'(erlang:element(2, Global), Timeout_ms) of
{ok, Binary} ->
distribute@codec:decode(erlang:element(4, Global), Binary);
{error, nil} ->
{error, decode_timeout}
end.