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(GMB) :: any() | {gleam_phantom, GMB}.
-opaque hub(GMC) :: {hub, hub_n(GMC)}.
-opaque hub2(GMD) :: {hub2, hub_n(GMD)}.
-opaque hub3(GME) :: {hub3, hub_n(GME)}.
-opaque hub4(GMF) :: {hub4, hub_n(GMF)}.
-spec new_n(fun((hub_n(any())) -> GMX)) -> GMX.
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(GMY), list(list(binary())), GMY) -> nil.
notify_n(Hub, Topics, Value) ->
event_hub_ffi:invoke_topic_based(Hub, Topics, Value).
-spec subscribe_n(hub_n(GNC), list(list(binary())), fun((GNC) -> 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())) -> GNJ)) -> GNJ.
new(Context) ->
new_n(fun(Hub) -> Context({hub, Hub}) end).
-spec notify(hub(GNK), list(binary()), GNK) -> nil.
notify(Hub, Topics, Value) ->
notify_n(erlang:element(2, Hub), [Topics], Value).
-spec subscribe(hub(GNN), list(binary()), fun((GNN) -> nil)) -> fun(() -> nil).
subscribe(Hub, Topics, Callback) ->
subscribe_n(erlang:element(2, Hub), [Topics], Callback).
-spec new2(fun((hub2(any())) -> GNT)) -> GNT.
new2(Context) ->
new_n(fun(Hub) -> Context({hub2, Hub}) end).
-spec notify2(hub2(GNU), list(binary()), list(binary()), GNU) -> nil.
notify2(Hub, Topics1, Topics2, Value) ->
notify_n(erlang:element(2, Hub), [Topics1, Topics2], Value).
-spec subscribe2(hub2(GNY), list(binary()), list(binary()), fun((GNY) -> nil)) -> fun(() -> nil).
subscribe2(Hub, Topics1, Topics2, Callback) ->
subscribe_n(erlang:element(2, Hub), [Topics1, Topics2], Callback).
-spec new3(fun((hub3(any())) -> GOF)) -> GOF.
new3(Context) ->
new_n(fun(Hub) -> Context({hub3, Hub}) end).
-spec notify3(hub3(GOG), list(binary()), list(binary()), list(binary()), GOG) -> nil.
notify3(Hub, Topics1, Topics2, Topics3, Value) ->
notify_n(erlang:element(2, Hub), [Topics1, Topics2, Topics3], Value).
-spec subscribe3(
hub3(GOL),
list(binary()),
list(binary()),
list(binary()),
fun((GOL) -> nil)
) -> fun(() -> nil).
subscribe3(Hub, Topics1, Topics2, Topics3, Callback) ->
subscribe_n(erlang:element(2, Hub), [Topics1, Topics2, Topics3], Callback).
-spec new4(fun((hub4(any())) -> GOT)) -> GOT.
new4(Context) ->
new_n(fun(Hub) -> Context({hub4, Hub}) end).
-spec notify4(
hub4(GOU),
list(binary()),
list(binary()),
list(binary()),
list(binary()),
GOU
) -> nil.
notify4(Hub, Topics1, Topics2, Topics3, Topics4, Value) ->
notify_n(
erlang:element(2, Hub),
[Topics1, Topics2, Topics3, Topics4],
Value
).
-spec subscribe4(
hub4(GPA),
list(binary()),
list(binary()),
list(binary()),
list(binary()),
fun((GPA) -> nil)
) -> fun(() -> nil).
subscribe4(Hub, Topics1, Topics2, Topics3, Topics4, Callback) ->
subscribe_n(
erlang:element(2, Hub),
[Topics1, Topics2, Topics3, Topics4],
Callback
).