Packages

Event-Hub is a Gleam library that provides simple hubs with publishers and subscribers for event-driven observers. It supports asynchronous message handling and event notifications, decoupling components efficiently. It works on Erlang and JavaScript.

Current section

Files

Jump to
event_hub src event_hub@topic.erl
Raw

src/event_hub@topic.erl

-module(event_hub@topic).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_n/1, notify_n/3, subscribe_n/3, new/1, notify/3, subscribe/3, new2/1, notify2/4, subscribe2/4, new3/1, notify3/5, subscribe3/5, new4/1, notify4/6, subscribe4/6]).
-export_type([hub_n/1, hub/1, hub2/1, hub3/1, hub4/1]).
-type hub_n(FZR) :: any() | {gleam_phantom, FZR}.
-opaque hub(FZS) :: {hub, hub_n(FZS)}.
-opaque hub2(FZT) :: {hub2, hub_n(FZT)}.
-opaque hub3(FZU) :: {hub3, hub_n(FZU)}.
-opaque hub4(FZV) :: {hub4, hub_n(FZV)}.
-spec new_n(fun((hub_n(any())) -> GAN)) -> GAN.
new_n(Context) ->
Hub = event_hub_ffi:start_topic_based(),
Result = Context(Hub),
event_hub_ffi:stop_topic_based(Hub),
Result.
-spec notify_n(hub_n(GAO), list(list(binary())), GAO) -> nil.
notify_n(Hub, Topics, Value) ->
event_hub_ffi:invoke_topic_based(Hub, Topics, Value).
-spec subscribe_n(hub_n(GAS), list(list(binary())), fun((GAS) -> nil)) -> fun(() -> nil).
subscribe_n(Hub, Topics, Callback) ->
Index = event_hub_ffi:add_topic_based(Hub, Topics, Callback),
fun() -> event_hub_ffi:remove_topic_based(Hub, Index) end.
-spec new(fun((hub(any())) -> GAZ)) -> GAZ.
new(Context) ->
new_n(fun(Hub) -> Context({hub, Hub}) end).
-spec notify(hub(GBA), list(binary()), GBA) -> nil.
notify(Hub, Topics, Value) ->
notify_n(erlang:element(2, Hub), [Topics], Value).
-spec subscribe(hub(GBD), list(binary()), fun((GBD) -> nil)) -> fun(() -> nil).
subscribe(Hub, Topics, Callback) ->
subscribe_n(erlang:element(2, Hub), [Topics], Callback).
-spec new2(fun((hub2(any())) -> GBJ)) -> GBJ.
new2(Context) ->
new_n(fun(Hub) -> Context({hub2, Hub}) end).
-spec notify2(hub2(GBK), list(binary()), list(binary()), GBK) -> nil.
notify2(Hub, Topics1, Topics2, Value) ->
notify_n(erlang:element(2, Hub), [Topics1, Topics2], Value).
-spec subscribe2(hub2(GBO), list(binary()), list(binary()), fun((GBO) -> nil)) -> fun(() -> nil).
subscribe2(Hub, Topics1, Topics2, Callback) ->
subscribe_n(erlang:element(2, Hub), [Topics1, Topics2], Callback).
-spec new3(fun((hub3(any())) -> GBV)) -> GBV.
new3(Context) ->
new_n(fun(Hub) -> Context({hub3, Hub}) end).
-spec notify3(hub3(GBW), list(binary()), list(binary()), list(binary()), GBW) -> nil.
notify3(Hub, Topics1, Topics2, Topics3, Value) ->
notify_n(erlang:element(2, Hub), [Topics1, Topics2, Topics3], Value).
-spec subscribe3(
hub3(GCB),
list(binary()),
list(binary()),
list(binary()),
fun((GCB) -> nil)
) -> fun(() -> nil).
subscribe3(Hub, Topics1, Topics2, Topics3, Callback) ->
subscribe_n(erlang:element(2, Hub), [Topics1, Topics2, Topics3], Callback).
-spec new4(fun((hub4(any())) -> GCJ)) -> GCJ.
new4(Context) ->
new_n(fun(Hub) -> Context({hub4, Hub}) end).
-spec notify4(
hub4(GCK),
list(binary()),
list(binary()),
list(binary()),
list(binary()),
GCK
) -> nil.
notify4(Hub, Topics1, Topics2, Topics3, Topics4, Value) ->
notify_n(
erlang:element(2, Hub),
[Topics1, Topics2, Topics3, Topics4],
Value
).
-spec subscribe4(
hub4(GCQ),
list(binary()),
list(binary()),
list(binary()),
list(binary()),
fun((GCQ) -> nil)
) -> fun(() -> nil).
subscribe4(Hub, Topics1, Topics2, Topics3, Topics4, Callback) ->
subscribe_n(
erlang:element(2, Hub),
[Topics1, Topics2, Topics3, Topics4],
Callback
).