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.erl
Raw

src/event_hub.erl

-module(event_hub).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/1, notify/2, subscribe/2]).
-export_type([hub/1]).
-type hub(FXZ) :: any() | {gleam_phantom, FXZ}.
-spec new(fun((hub(any())) -> FYO)) -> FYO.
new(Context) ->
Hub = event_hub_ffi:start_stateless(),
Result = Context(Hub),
event_hub_ffi:stop_stateless(Hub),
Result.
-spec notify(hub(FYP), FYP) -> nil.
notify(Hub, Value) ->
event_hub_ffi:invoke_stateless(Hub, Value).
-spec subscribe(hub(FYR), fun((FYR) -> nil)) -> fun(() -> nil).
subscribe(Hub, Callback) ->
Index = event_hub_ffi:add_stateless(Hub, Callback),
fun() -> event_hub_ffi:remove_stateless(Hub, Index) end.