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

src/event_hub@reactive.erl

-module(event_hub@reactive).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/2, notify/1, subscribe/2]).
-export_type([hub/1]).
-opaque hub(GJP) :: {hub, event_hub:hub(GJP), fun(() -> GJP)}.
-spec new(fun(() -> GJR), fun((hub(GJR)) -> GJU)) -> GJU.
new(State, Context) ->
event_hub:new(
fun(Inner) ->
Hub = {hub, Inner, State},
Context(Hub)
end
).
-spec notify(hub(GJV)) -> GJV.
notify(Hub) ->
Value = (erlang:element(3, Hub))(),
event_hub:notify(erlang:element(2, Hub), Value),
Value.
-spec subscribe(hub(GJX), fun((GJX) -> nil)) -> fun(() -> nil).
subscribe(Hub, Callback) ->
event_hub:subscribe(erlang:element(2, Hub), Callback).