Current section

Files

Jump to
glixir src glixir@syn_pubsub.erl
Raw

src/glixir@syn_pubsub.erl

-module(glixir@syn_pubsub).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glixir/syn_pubsub.gleam").
-export([subscribe/4, subscribe_with_registry_key/5, publish/3]).
-export_type([syn_pub_sub_error/0, syn_pub_sub_subscribe_result/0, syn_pub_sub_publish_result/0]).
-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(
"\n"
" Syn PubSub bridge for Gleam actors.\n"
"\n"
" Similar to glixir/pubsub but uses :syn instead of Phoenix.PubSub.\n"
" Messages are passed as strings (typically JSON) for type-safe interop.\n"
"\n"
).
-type syn_pub_sub_error() :: {subscribe_error, binary()} |
{publish_error, binary()}.
-type syn_pub_sub_subscribe_result() :: syn_pubsub_subscribe_ok |
{syn_pubsub_subscribe_error, gleam@dynamic:dynamic_()}.
-type syn_pub_sub_publish_result() :: {syn_pubsub_publish_ok, integer()} |
{syn_pubsub_publish_error, gleam@dynamic:dynamic_()}.
-file("src/glixir/syn_pubsub.gleam", 69).
?DOC(
" Subscribe to a syn pubsub group with message handling\n"
" The gleam_function must accept a single String parameter (typically JSON) and return Nil\n"
"\n"
" ## Example\n"
" ```gleam\n"
" syn_pubsub.subscribe(\n"
" \"job_completions\",\n"
" \"all\",\n"
" \"actors@url_queue_actor\",\n"
" \"handle_job_cancellation\"\n"
" )\n"
" ```\n"
).
-spec subscribe(binary(), binary(), binary(), binary()) -> {ok, nil} |
{error, syn_pub_sub_error()}.
subscribe(Scope, Group, Gleam_module, Gleam_function) ->
glixir@utils:debug_log_with_prefix(
debug,
<<"syn_pubsub"/utf8>>,
<<<<<<"Subscribing to "/utf8, Scope/binary>>/binary, "/"/utf8>>/binary,
Group/binary>>
),
case 'Elixir.Glixir.SynPubSub':subscribe(
Scope,
Group,
Gleam_module,
Gleam_function
) of
syn_pubsub_subscribe_ok ->
glixir@utils:debug_log_with_prefix(
info,
<<"syn_pubsub"/utf8>>,
<<<<<<"Subscribed successfully to: "/utf8, Scope/binary>>/binary,
"/"/utf8>>/binary,
Group/binary>>
),
{ok, nil};
{syn_pubsub_subscribe_error, Reason} ->
glixir@utils:debug_log_with_prefix(
error,
<<"syn_pubsub"/utf8>>,
<<<<<<"Subscribe failed for "/utf8, Scope/binary>>/binary,
"/"/utf8>>/binary,
Group/binary>>
),
{error, {subscribe_error, gleam@string:inspect(Reason)}}
end.
-file("src/glixir/syn_pubsub.gleam", 114).
?DOC(
" Subscribe to a syn pubsub group with registry key for direct actor targeting\n"
" The gleam_function must accept two String parameters (registry_key, message) and return Nil\n"
"\n"
" ## Example\n"
" ```gleam\n"
" syn_pubsub.subscribe_with_registry_key(\n"
" \"job_completions\",\n"
" \"all\",\n"
" \"actors@url_queue_actor\",\n"
" \"handle_job_cancellation\",\n"
" \"https://api.example.com/endpoint\"\n"
" )\n"
" ```\n"
).
-spec subscribe_with_registry_key(
binary(),
binary(),
binary(),
binary(),
binary()
) -> {ok, nil} | {error, syn_pub_sub_error()}.
subscribe_with_registry_key(
Scope,
Group,
Gleam_module,
Gleam_function,
Registry_key
) ->
glixir@utils:debug_log_with_prefix(
debug,
<<"syn_pubsub"/utf8>>,
<<<<<<<<<<"Subscribing to "/utf8, Scope/binary>>/binary, "/"/utf8>>/binary,
Group/binary>>/binary,
" with key: "/utf8>>/binary,
Registry_key/binary>>
),
case 'Elixir.Glixir.SynPubSub':subscribe(
Scope,
Group,
Gleam_module,
Gleam_function,
Registry_key
) of
syn_pubsub_subscribe_ok ->
glixir@utils:debug_log_with_prefix(
info,
<<"syn_pubsub"/utf8>>,
<<<<<<<<<<"Subscribed successfully to: "/utf8, Scope/binary>>/binary,
"/"/utf8>>/binary,
Group/binary>>/binary,
" with key: "/utf8>>/binary,
Registry_key/binary>>
),
{ok, nil};
{syn_pubsub_subscribe_error, Reason} ->
glixir@utils:debug_log_with_prefix(
error,
<<"syn_pubsub"/utf8>>,
<<<<<<"Subscribe failed for "/utf8, Scope/binary>>/binary,
"/"/utf8>>/binary,
Group/binary>>
),
{error, {subscribe_error, gleam@string:inspect(Reason)}}
end.
-file("src/glixir/syn_pubsub.gleam", 175).
?DOC(
" Publish a message to all subscribers of a syn pubsub group\n"
" Returns the number of processes the message was delivered to\n"
"\n"
" ## Example\n"
" ```gleam\n"
" syn_pubsub.publish(\"job_completions\", \"all\", job_id)\n"
" |> result.map(fn(count) {\n"
" logging.log(Info, \"Sent to \" <> int.to_string(count) <> \" subscribers\")\n"
" })\n"
" ```\n"
).
-spec publish(binary(), binary(), binary()) -> {ok, integer()} |
{error, syn_pub_sub_error()}.
publish(Scope, Group, Message) ->
glixir@utils:debug_log_with_prefix(
debug,
<<"syn_pubsub"/utf8>>,
<<<<<<"Publishing to "/utf8, Scope/binary>>/binary, "/"/utf8>>/binary,
Group/binary>>
),
case 'Elixir.Glixir.SynPubSub':publish(Scope, Group, Message) of
{syn_pubsub_publish_ok, Count} ->
glixir@utils:debug_log_with_prefix(
info,
<<"syn_pubsub"/utf8>>,
<<<<"Published to "/utf8, (gleam@string:inspect(Count))/binary>>/binary,
" subscribers"/utf8>>
),
{ok, Count};
{syn_pubsub_publish_error, Reason} ->
glixir@utils:debug_log_with_prefix(
error,
<<"syn_pubsub"/utf8>>,
<<<<<<"Publish failed for "/utf8, Scope/binary>>/binary,
"/"/utf8>>/binary,
Group/binary>>
),
{error, {publish_error, gleam@string:inspect(Reason)}}
end.