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

src/event_hub@filtered.erl

-module(event_hub@filtered).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/event_hub/filtered.gleam").
-export([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/2, hub2/3, hub3/4, hub4/5]).
-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 `filtered` module provides a way to manage and notify subscribers about events based on specific topics.\n"
" It supports creating observers that can filter events using topics, allowing more fine-grained control over event handling.\n"
" If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead.\n"
" \n"
" ## Examples\n"
"\n"
" ### Filtered Observer\n"
" ```gleam\n"
" import gleam/io\n"
" import event_hub/filtered\n"
" \n"
" pub fn main() {\n"
" use hub <- filtered.new()\n"
" \n"
" let unsubscribe_a =\n"
" filtered.subscribe(hub, [Ok(\"topic1\")], fn(value) {\n"
" io.println(\"A received: \" <> value)\n"
" })\n"
" \n"
" let unsubscribe_b =\n"
" filtered.subscribe(hub, [Error(\"topic2\")], fn(value) {\n"
" io.println(\"B received: \" <> value)\n"
" })\n"
" \n"
" filtered.notify(hub, [Ok(\"topic1\")], \"Message for topic1\")\n"
" filtered.notify(hub, [Error(\"topic2\")], \"Message for topic2\")\n"
" \n"
" unsubscribe_a()\n"
" unsubscribe_b()\n"
" }\n"
" ```\n"
).
-opaque hub(DUS, DUT) :: {hub, event_hub:hub({list(DUT), DUS})}.
-opaque hub2(DUU, DUV, DUW) :: {hub2, hub({list(DUW), DUU}, DUV)}.
-opaque hub3(DUX, DUY, DUZ, DVA) :: {hub3, hub2({list(DVA), DUX}, DUY, DUZ)}.
-opaque hub4(DVB, DVC, DVD, DVE, DVF) :: {hub4,
hub3({list(DVF), DVB}, DVC, DVD, DVE)}.
-file("src/event_hub/filtered.gleam", 65).
?DOC(
" Creates a new filtered event hub, executes the given context with the hub.\n"
" If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead.\n"
"\n"
" ## Parameters\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/filtered\n"
"\n"
" pub fn example() {\n"
" filtered.new(fn(hub) {\n"
" // Use the hub\n"
" Nil\n"
" })\n"
" }\n"
" ```\n"
).
-spec new(fun((hub(any(), any())) -> DVN)) -> DVN.
new(Context) ->
event_hub:new(
fun(Hub) ->
Hub@1 = {hub, Hub},
Context(Hub@1)
end
).
-file("src/event_hub/filtered.gleam", 88).
?DOC(
" Notifies subscribers of the hub about an event with the given topics and 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"
" - `topics`: The list of topics associated with the event.\n"
" - `value`: The value to send to all subscribers.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/filtered\n"
"\n"
" pub fn example(hub: filtered.Hub(String, String)) {\n"
" filtered.notify(hub, [\"topic1\"], \"event\")\n"
" }\n"
" ```\n"
).
-spec notify(hub(DVO, DVP), list(DVP), DVO) -> nil.
notify(Hub, Topics, Value) ->
event_hub:notify(erlang:element(2, Hub), {Topics, Value}).
-file("src/event_hub/filtered.gleam", 122).
?DOC(
" Subscribes to specific topics and returns an unsubscribe function.\n"
" The callback will be invoked only if the event topics match the subscription topics.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub` to add the callback to.\n"
" - `topics`: The list of topics to subscribe to.\n"
" - `callback`: The callback function to invoke when an event with matching topics occurs.\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/filtered\n"
" \n"
" pub fn example(hub: filtered.Hub(String, String)) {\n"
" let unsubscribe =\n"
" filtered.subscribe(hub, [\"topic1\"], fn(value) {\n"
" io.println(\"Received value: \" <> value)\n"
" })\n"
" \n"
" // To unsubscribe\n"
" unsubscribe()\n"
" }\n"
" ```\n"
).
-spec subscribe(hub(DVT, DVU), list(DVU), fun((DVT) -> nil)) -> fun(() -> nil).
subscribe(Hub, Topics, Callback) ->
Topics@1 = gleam@set:from_list(Topics),
event_hub:subscribe(
erlang:element(2, Hub),
fun(_use0) ->
{Event_topics, Event_value} = _use0,
case gleam@list:any(
Event_topics,
fun(Topic) -> gleam@set:contains(Topics@1, Topic) end
) of
true ->
Callback(Event_value);
false ->
nil
end
end
).
-file("src/event_hub/filtered.gleam", 162).
?DOC(
" Creates a new filtered event hub with two levels of topics.\n"
" If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead.\n"
"\n"
" ## Parameters\n"
" - `context`: A function that takes the created `Hub2` and returns a result.\n"
"\n"
" ## Returns\n"
" The result of executing the context function.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/filtered\n"
"\n"
" pub fn example() {\n"
" filtered.new2(fn(hub) {\n"
" // Use the hub\n"
" Nil\n"
" })\n"
" }\n"
" ```\n"
).
-spec new2(fun((hub2(any(), any(), any())) -> DWF)) -> DWF.
new2(Context) ->
new(
fun(Hub) ->
Hub@1 = {hub2, Hub},
Context(Hub@1)
end
).
-file("src/event_hub/filtered.gleam", 187).
?DOC(
" Notifies subscribers of the hub about an event with two levels of topics and a value.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub2` to notify.\n"
" - `topics1`: The first level of topics associated with the event.\n"
" - `topics2`: The second level of topics associated with the event.\n"
" - `value`: The value to send to all subscribers.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/filtered\n"
" \n"
" pub fn example(hub: filtered.Hub2(String, String, String)) {\n"
" filtered.notify2(hub, [\"topic1\"], [\"subtopic1\"], \"event\")\n"
" }\n"
" ```\n"
).
-spec notify2(hub2(DWG, DWH, DWI), list(DWH), list(DWI), DWG) -> nil.
notify2(Hub, Topics1, Topics2, Value) ->
notify(erlang:element(2, Hub), Topics1, {Topics2, Value}).
-file("src/event_hub/filtered.gleam", 222).
?DOC(
" Subscribes to specific topics in a two-level hierarchy and returns an unsubscribe function.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub2` to add the callback to.\n"
" - `topics1`: The first level of topics to subscribe to.\n"
" - `topics2`: The second level of topics to subscribe to.\n"
" - `callback`: The callback function to invoke when an event with matching topics occurs.\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/filtered\n"
" \n"
" pub fn example(hub: filtered.Hub2(String, String, String)) {\n"
" let unsubscribe =\n"
" filtered.subscribe2(hub, [\"topic1\"], [\"subtopic1\"], fn(value) {\n"
" io.println(\"Received value: \" <> value)\n"
" })\n"
" \n"
" // To unsubscribe\n"
" unsubscribe()\n"
" }\n"
" ```\n"
).
-spec subscribe2(hub2(DWO, DWP, DWQ), list(DWP), list(DWQ), fun((DWO) -> nil)) -> fun(() -> nil).
subscribe2(Hub, Topics1, Topics2, Callback) ->
Topics = gleam@set:from_list(Topics2),
subscribe(
erlang:element(2, Hub),
Topics1,
fun(_use0) ->
{Event_topics, Event_value} = _use0,
case gleam@list:any(
Event_topics,
fun(Topic) -> gleam@set:contains(Topics, Topic) end
) of
true ->
Callback(Event_value);
false ->
nil
end
end
).
-file("src/event_hub/filtered.gleam", 265).
?DOC(
" Creates a new filtered event hub with three levels of topics.\n"
" If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead.\n"
"\n"
" ## Parameters\n"
" - `context`: A function that takes the created `Hub3` and returns a result.\n"
"\n"
" ## Returns\n"
" The result of executing the context function.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/filtered\n"
"\n"
" pub fn example() {\n"
" filtered.new3(fn(hub) {\n"
" // Use the hub\n"
" Nil\n"
" })\n"
" }\n"
" ```\n"
).
-spec new3(fun((hub3(any(), any(), any(), any())) -> DXF)) -> DXF.
new3(Context) ->
new2(
fun(Hub) ->
Hub@1 = {hub3, Hub},
Context(Hub@1)
end
).
-file("src/event_hub/filtered.gleam", 292).
?DOC(
" Notifies subscribers of the hub about an event with three levels of topics and a value.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub3` to notify.\n"
" - `topics1`: The first level of topics associated with the event.\n"
" - `topics2`: The second level of topics associated with the event.\n"
" - `topics3`: The third level of topics associated with the event.\n"
" - `value`: The value to send to all subscribers.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/filtered\n"
"\n"
" pub fn example(hub: filtered.Hub3(String, String, String, String)) {\n"
" filtered.notify3(hub, [\"topic1\"], [\"subtopic1\"], [\"subsubtopic1\"], \"event\")\n"
" }\n"
" ```\n"
).
-spec notify3(hub3(DXG, DXH, DXI, DXJ), list(DXH), list(DXI), list(DXJ), DXG) -> nil.
notify3(Hub, Topics1, Topics2, Topics3, Value) ->
notify2(erlang:element(2, Hub), Topics1, Topics2, {Topics3, Value}).
-file("src/event_hub/filtered.gleam", 333).
?DOC(
" Subscribes to specific topics in a three-level hierarchy and returns an unsubscribe function.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub3` to add the callback to.\n"
" - `topics1`: The first level of topics to subscribe to.\n"
" - `topics2`: The second level of topics to subscribe to.\n"
" - `topics3`: The third level of topics to subscribe to.\n"
" - `callback`: The callback function to invoke when an event with matching topics occurs.\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/filtered\n"
" \n"
" pub fn example(hub: filtered.Hub3(String, String, String, String)) {\n"
" let unsubscribe =\n"
" filtered.subscribe3(\n"
" hub,\n"
" [\"topic1\"],\n"
" [\"subtopic1\"],\n"
" [\"subsubtopic1\"],\n"
" fn(value) { io.println(\"Received value: \" <> value) },\n"
" )\n"
" \n"
" // To unsubscribe\n"
" unsubscribe()\n"
" }\n"
" ```\n"
).
-spec subscribe3(
hub3(DXR, DXS, DXT, DXU),
list(DXS),
list(DXT),
list(DXU),
fun((DXR) -> nil)
) -> fun(() -> nil).
subscribe3(Hub, Topics1, Topics2, Topics3, Callback) ->
Topics = gleam@set:from_list(Topics3),
subscribe2(
erlang:element(2, Hub),
Topics1,
Topics2,
fun(_use0) ->
{Event_topics, Event_value} = _use0,
case gleam@list:any(
Event_topics,
fun(Topic) -> gleam@set:contains(Topics, Topic) end
) of
true ->
Callback(Event_value);
false ->
nil
end
end
).
-file("src/event_hub/filtered.gleam", 388).
?DOC(
" Creates a new filtered event hub with four levels of topics.\n"
" If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead.\n"
"\n"
" ## Parameters\n"
" - `context`: A function that takes the created `Hub4` and returns a result.\n"
"\n"
" ## Returns\n"
" The result of executing the context function.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/filtered\n"
"\n"
" pub fn example() {\n"
" filtered.new4(fn(hub) {\n"
" // Use the hub\n"
" Nil\n"
" })\n"
" }\n"
" ```\n"
).
-spec new4(fun((hub4(any(), any(), any(), any(), any())) -> DYN)) -> DYN.
new4(Context) ->
new3(
fun(Hub) ->
Hub@1 = {hub4, Hub},
Context(Hub@1)
end
).
-file("src/event_hub/filtered.gleam", 425).
?DOC(
" Notifies subscribers of the hub about an event with four levels of topics and a value.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub4` to notify.\n"
" - `topics1`: The first level of topics associated with the event.\n"
" - `topics2`: The second level of topics associated with the event.\n"
" - `topics3`: The third level of topics associated with the event.\n"
" - `topics4`: The fourth level of topics associated with the event.\n"
" - `value`: The value to send to all subscribers.\n"
"\n"
" ## Example\n"
" ```gleam\n"
" import event_hub/filtered\n"
" \n"
" pub fn example(hub: filtered.Hub4(String, String, String, String, String)) {\n"
" filtered.notify4(\n"
" hub,\n"
" [\"topic1\"],\n"
" [\"subtopic1\"],\n"
" [\"subsubtopic1\"],\n"
" [\"subsubsubtopic1\"],\n"
" \"event\",\n"
" )\n"
" }\n"
" ```\n"
).
-spec notify4(
hub4(DYO, DYP, DYQ, DYR, DYS),
list(DYP),
list(DYQ),
list(DYR),
list(DYS),
DYO
) -> nil.
notify4(Hub, Topics1, Topics2, Topics3, Topics4, Value) ->
notify3(erlang:element(2, Hub), Topics1, Topics2, Topics3, {Topics4, Value}).
-file("src/event_hub/filtered.gleam", 469).
?DOC(
" Subscribes to specific topics in a four-level hierarchy and returns an unsubscribe function.\n"
"\n"
" ## Parameters\n"
" - `hub`: The `Hub4` to add the callback to.\n"
" - `topics1`: The first level of topics to subscribe to.\n"
" - `topics2`: The second level of topics to subscribe to.\n"
" - `topics3`: The third level of topics to subscribe to.\n"
" - `topics4`: The fourth level of topics to subscribe to.\n"
" - `callback`: The callback function to invoke when an event with matching topics occurs.\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/filtered\n"
" \n"
" pub fn example(hub: filtered.Hub4(String, String, String, String, String)) {\n"
" let unsubscribe =\n"
" filtered.subscribe4(\n"
" hub,\n"
" [\"topic1\"],\n"
" [\"subtopic1\"],\n"
" [\"subsubtopic1\"],\n"
" [\"subsubsubtopic1\"],\n"
" fn(value) { io.println(\"Received value: \" <> value) },\n"
" )\n"
" \n"
" // To unsubscribe\n"
" unsubscribe()\n"
" }\n"
" ```\n"
).
-spec subscribe4(
hub4(DZC, DZD, DZE, DZF, DZG),
list(DZD),
list(DZE),
list(DZF),
list(DZG),
fun((DZC) -> nil)
) -> fun(() -> nil).
subscribe4(Hub, Topics1, Topics2, Topics3, Topics4, Callback) ->
Topics = gleam@set:from_list(Topics4),
subscribe3(
erlang:element(2, Hub),
Topics1,
Topics2,
Topics3,
fun(_use0) ->
{Event_topics, Event_value} = _use0,
case gleam@list:any(
Event_topics,
fun(Topic) -> gleam@set:contains(Topics, Topic) end
) of
true ->
Callback(Event_value);
false ->
nil
end
end
).