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
Current section
Files
src/event_hub@reactive.erl
-module(event_hub@reactive).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/event_hub/reactive.gleam").
-export([new/2, notify/1, subscribe/2]).
-export_type([hub/1]).
-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(
" The `reactive` module provides a way to manage and notify subscribers about events based on a state that can be\n"
" dynamically retrieved. It supports creating observers that can react to changes in state, which can be useful for\n"
" broadcasting things that constantly change like the current time or database updates.\n"
"\n"
" ## Examples\n"
"\n"
" ### Reactive Observer\n"
" ```gleam\n"
" import gleam/io\n"
" import event_hub/reactive\n"
" \n"
" pub fn main() {\n"
" let get_time = fn() { \"2024-05-21T16:30:00Z\" }\n"
" \n"
" use hub <- reactive.new(get_time)\n"
" let unsubscribe =\n"
" reactive.subscribe(hub, fn(value) { io.println(\"Current time: \" <> value) })\n"
" \n"
" reactive.notify(hub)\n"
" \n"
" unsubscribe()\n"
" reactive.notify(hub)\n"
" }\n"
" ```\n"
).
-opaque hub(EEK) :: {hub, event_hub:hub(EEK), fun(() -> EEK)}.
-file("src/event_hub/reactive.gleam", 59).
?DOC(
" Creates a new reactive observer hub, executes the given context with the hub.\n"
"\n"
" ## Parameters\n"
" - `state`: A function that returns the current state value.\n"
" - `context`: A function that takes the created `Hub` and returns a result.\n"
"\n"
" ## Returns\n"
" The result of executing the context function.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/reactive\n"
"\n"
" pub fn example() {\n"
" let get_state = fn() { \"current state\" }\n"
"\n"
" reactive.new(get_state, fn(hub) {\n"
" // Use the hub\n"
" Nil\n"
" })\n"
" }\n"
" ```\n"
).
-spec new(fun(() -> EEM), fun((hub(EEM)) -> EEP)) -> EEP.
new(State, Context) ->
event_hub:new(
fun(Inner) ->
Hub = {hub, Inner, State},
Context(Hub)
end
).
-file("src/event_hub/reactive.gleam", 86).
?DOC(
" Notifies subscribers of the hub about an event with the current state value.\n"
" These notifications occur in parallel but `notify` waits for all of them to complete.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub` to notify.\n"
"\n"
" ## Returns\n"
" The current state value.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/reactive\n"
"\n"
" pub fn example(hub: reactive.Hub(String)) {\n"
" let value = reactive.notify(hub)\n"
" }\n"
" ```\n"
).
-spec notify(hub(EEQ)) -> EEQ.
notify(Hub) ->
Value = (erlang:element(3, Hub))(),
event_hub:notify(erlang:element(2, Hub), Value),
Value.
-file("src/event_hub/reactive.gleam", 117).
?DOC(
" Subscribes to state changes and returns an unsubscribe function.\n"
" The callback will be invoked with the current state value when `notify` is called.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub` to add the callback to.\n"
" - `callback`: The callback function to invoke with the current state value.\n"
"\n"
" ## Returns\n"
" An `Unsubscribe` function that can be called to remove the callback.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import gleam/io\n"
" import event_hub/reactive\n"
" \n"
" pub fn example(hub: reactive.Hub(String)) {\n"
" let unsubscribe =\n"
" reactive.subscribe(hub, fn(value) {\n"
" io.println(\"Received state: \" <> value)\n"
" })\n"
" \n"
" // To unsubscribe\n"
" unsubscribe()\n"
" }\n"
" ```\n"
).
-spec subscribe(hub(EES), fun((EES) -> nil)) -> fun(() -> nil).
subscribe(Hub, Callback) ->
event_hub:subscribe(erlang:element(2, Hub), Callback).