From b54ec4e08707f4d90351514d0475dc6cbd6292fa Mon Sep 17 00:00:00 2001 From: Felix Mayer Date: Wed, 22 May 2024 08:05:19 +0200 Subject: [PATCH] Rename to event_hub --- CHANGELOG.md | 2 +- README.md | 50 +++++------ gleam.toml | 10 +-- package-lock.json | 4 +- package.json | 25 +++--- src/{observer.gleam => event_hub.gleam} | 48 +++++------ src/{observer => event_hub}/filtered.gleam | 70 +++++++-------- src/{observer => event_hub}/reactive.gleam | 22 ++--- src/{observer => event_hub}/stateful.gleam | 42 ++++----- src/{observer => event_hub}/topic.gleam | 86 +++++++++---------- src/{observer_ffi.erl => event_hub_ffi.erl} | 8 +- src/{observer_ffi.mjs => event_hub_ffi.mjs} | 10 +-- .../event_hub_examples.gleam | 30 +++---- ...server_test.gleam => event_hub_test.gleam} | 36 ++++---- 14 files changed, 220 insertions(+), 223 deletions(-) rename src/{observer.gleam => event_hub.gleam} (71%) rename src/{observer => event_hub}/filtered.gleam (90%) rename src/{observer => event_hub}/reactive.gleam (88%) rename src/{observer => event_hub}/stateful.gleam (82%) rename src/{observer => event_hub}/topic.gleam (87%) rename src/{observer_ffi.erl => event_hub_ffi.erl} (98%) rename src/{observer_ffi.mjs => event_hub_ffi.mjs} (96%) rename src/observer_examples.gleam => test/event_hub_examples.gleam (95%) rename test/{observer_test.gleam => event_hub_test.gleam} (96%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46c866d..e510f90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # Changelog -## Version 1.0.0 +## 22.05.2024 - Version 1.0.0 - Initial release diff --git a/README.md b/README.md index c2f0210..706f0e9 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,27 @@ -# Observer +# Event-Hub -[![Package Version](https://img.shields.io/hexpm/v/observer)](https://hex.pm/packages/observer) -[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/observer/) +[![Package Version](https://img.shields.io/hexpm/v/event_hub)](https://hex.pm/packages/event_hub) +[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/event_hub/) -Observer is a Gleam library that provides simple hubs with publishers and +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. --- -Further documentation can be found at . +Further documentation can be found at . -- [observer](https://hexdocs.pm/observer/observer.html) -- [observer/filtered](https://hexdocs.pm/observer/observer/filtered.html) -- [observer/reactive](https://hexdocs.pm/observer/observer/reactive.html) -- [observer/stateful](https://hexdocs.pm/observer/observer/stateful.html) -- [observer/topic](https://hexdocs.pm/observer/observer/topic.html) +- [event_hub](https://hexdocs.pm/event_hub/event_hub.html) +- [event_hub/filtered](https://hexdocs.pm/event_hub/event_hub/filtered.html) +- [event_hub/reactive](https://hexdocs.pm/event_hub/event_hub/reactive.html) +- [event_hub/stateful](https://hexdocs.pm/event_hub/event_hub/stateful.html) +- [event_hub/topic](https://hexdocs.pm/event_hub/event_hub/topic.html) ## Try it yourself! ```sh -gleam add observer +gleam add event_hub ``` ## Examples @@ -31,18 +31,18 @@ gleam add observer ```gleam import gleam/int import gleam/io -import observer -import observer/filtered -import observer/reactive -import observer/stateful -import observer/topic +import event_hub +import event_hub/filtered +import event_hub/reactive +import event_hub/stateful +import event_hub/topic ``` ### Simple Observer ````gleam /// A simple observer implementation. -/// This example demonstrates the basic usage of the observer library. +/// This example demonstrates the basic usage of the Event-Hub library. /// It is an easy way to use publishers and subscribers to handle events. /// /// Outputs the following: @@ -53,15 +53,15 @@ import observer/topic /// ``` fn simple_observer() { // Creates a new hub for distributing events. - use hub <- observer.new() + use hub <- event_hub.new() // Notifies all subscribers of the hub that an event has occurred. - observer.notify(hub, 1) + event_hub.notify(hub, 1) // You can forward the hub to other functions or components. { // Using syntactic sugar for handling the callback. - use value <- observer.subscribe(hub) + use value <- event_hub.subscribe(hub) // This function gets called when the hub receives an event. io.println("[1] | Received an event with value: " <> int.to_string(value)) @@ -70,19 +70,19 @@ fn simple_observer() { // You can also subscribe using a normal callback function. // This also returns an unsubscribe function. let unsubscribe = - observer.subscribe(hub, fn(value) { + event_hub.subscribe(hub, fn(value) { io.println("[2] | Received an event with value: " <> int.to_string(value)) }) // Notifies all subscribers of the hub that an event has occurred with the value `2`. // These notifications occur in parallel but notify waits for all of them to complete. - observer.notify(hub, 2) + event_hub.notify(hub, 2) // Unsubscribe if you no longer need to receive events. unsubscribe() // Notify again to demonstrate that the unsubscribe function works. - observer.notify(hub, 3) + event_hub.notify(hub, 3) } ```` @@ -90,7 +90,7 @@ fn simple_observer() { ````gleam /// A simple stateful observer implementation. -/// This is like the previous example, but it uses a stateful observer. +/// This is like the previous example, but it uses a stateful event_hub. /// /// Outputs the following: /// ```text @@ -318,7 +318,7 @@ fn multiple_topics() { ### Filtered Observer ````gleam -/// This example demonstrates the usage of the filtered observer. +/// This example demonstrates the usage of the filtered event_hub. /// It is an easy way to filter events based on a list of topics. /// They work in a similar way, but you can use generics to filter by any type. /// diff --git a/gleam.toml b/gleam.toml index 60df082..eab5b9f 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,18 +1,14 @@ -name = "observer" +name = "event_hub" version = "1.0.0" -description = "Observer 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." +description = "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." licences = ["MIT"] -repository = { type = "github", user = "yerTools", repo = "observer" } +repository = { type = "github", user = "yerTools", repo = "event_hub" } target = "erlang" gleam = ">= 1.0.0" -internal_modules = [ - "observer_examples", -] - [documentation] pages = [ { title = "Changelog", path = "changelog.html", source = "./CHANGELOG.md" }, diff --git a/package-lock.json b/package-lock.json index 00fba43..bce5ec6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "observer", + "name": "event_hub", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "observer", + "name": "event_hub", "version": "1.0.0", "license": "MIT" } diff --git a/package.json b/package.json index 8fa41d9..6a21a8f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "observer", + "name": "event_hub", "version": "1.0.0", - "description": "Observer 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.", + "description": "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.", "directories": { "test": "test" }, @@ -10,23 +10,24 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/yerTools/observer.git" + "url": "git+https://github.com/yerTools/event_hub.git" }, "keywords": [ - "observer", - "notifications", - "javascript", - "events", "erlang", - "publisher", + "events", + "events", + "gleam", + "javascript", + "notifications", + "observer", "observer-pattern", - "subscriber", - "gleam" + "publisher", + "subscriber" ], "author": "Felix Mayer", "license": "MIT", "bugs": { - "url": "https://github.com/yerTools/observer/issues" + "url": "https://github.com/yerTools/event_hub/issues" }, - "homepage": "https://github.com/yerTools/observer#readme" + "homepage": "https://github.com/yerTools/event_hub#readme" } diff --git a/src/observer.gleam b/src/event_hub.gleam similarity index 71% rename from src/observer.gleam rename to src/event_hub.gleam index 144e29e..988a97f 100644 --- a/src/observer.gleam +++ b/src/event_hub.gleam @@ -1,4 +1,4 @@ -//// The `observer` module provides a way to manage and notify subscribers about events. +//// The `event_hub` module provides a way to manage and notify subscribers about events. //// It supports stateless observers, allowing functions to be registered and invoked in parallel when an event occurs. //// //// ## Examples @@ -6,45 +6,45 @@ //// ### Simple Observer //// ```gleam //// import gleam/io -//// import observer +//// import event_hub //// //// pub fn main() { -//// use hub <- observer.new() +//// use hub <- event_hub.new() //// //// let unsubscribe = -//// observer.subscribe(hub, fn(value) { +//// event_hub.subscribe(hub, fn(value) { //// io.println("Received value: " <> value) //// }) //// -//// observer.notify(hub, "Hello, world!") +//// event_hub.notify(hub, "Hello, world!") //// unsubscribe() -//// observer.notify(hub, "This won't be received") +//// event_hub.notify(hub, "This won't be received") //// } //// ``` /// Starts the stateless observer process. -@external(erlang, "observer_ffi", "start_stateless") -@external(javascript, "./observer_ffi.mjs", "startStateless") +@external(erlang, "event_hub_ffi", "start_stateless") +@external(javascript, "./event_hub_ffi.mjs", "startStateless") fn start_stateless() -> Hub(value_type) /// Adds a callback to the stateless observer, returning the index. -@external(erlang, "observer_ffi", "add_stateless") -@external(javascript, "./observer_ffi.mjs", "addStateless") +@external(erlang, "event_hub_ffi", "add_stateless") +@external(javascript, "./event_hub_ffi.mjs", "addStateless") fn add_stateless(hub: Hub(value_type), callback: Callback(value_type)) -> Int /// Invokes all callbacks in parallel with the given value and waits for all of them to complete. -@external(erlang, "observer_ffi", "invoke_stateless") -@external(javascript, "./observer_ffi.mjs", "invokeStateless") +@external(erlang, "event_hub_ffi", "invoke_stateless") +@external(javascript, "./event_hub_ffi.mjs", "invokeStateless") fn invoke_stateless(hub: Hub(value_type), value: value_type) -> Nil /// Removes a callback by its index. -@external(erlang, "observer_ffi", "remove_stateless") -@external(javascript, "./observer_ffi.mjs", "removeStateless") +@external(erlang, "event_hub_ffi", "remove_stateless") +@external(javascript, "./event_hub_ffi.mjs", "removeStateless") fn remove_stateless(hub: Hub(value_type), index: Int) -> Nil /// Stops the stateless observer process. -@external(erlang, "observer_ffi", "stop_stateless") -@external(javascript, "./observer_ffi.mjs", "stopStateless") +@external(erlang, "event_hub_ffi", "stop_stateless") +@external(javascript, "./event_hub_ffi.mjs", "stopStateless") fn stop_stateless(hub: Hub(value_type)) -> Nil /// Represents a hub for managing event subscriptions and notifications. @@ -68,10 +68,10 @@ pub type Unsubscribe = /// /// ## Example /// ```gleam -/// import observer +/// import event_hub /// /// pub fn example() { -/// observer.new(fn(hub) { observer.notify(hub, "event") }) +/// event_hub.new(fn(hub) { event_hub.notify(hub, "event") }) /// } /// ``` pub fn new(in context: fn(Hub(value_type)) -> result) -> result { @@ -92,10 +92,10 @@ pub fn new(in context: fn(Hub(value_type)) -> result) -> result { /// /// ## Example /// ```gleam -/// import observer +/// import event_hub /// -/// pub fn example(hub: observer.Hub(String)) { -/// observer.notify(hub, "event") +/// pub fn example(hub: event_hub.Hub(String)) { +/// event_hub.notify(hub, "event") /// } /// ``` pub fn notify(on hub: Hub(value_type), with value: value_type) -> Nil { @@ -114,11 +114,11 @@ pub fn notify(on hub: Hub(value_type), with value: value_type) -> Nil { /// ## Example /// ```gleam /// import gleam/io -/// import observer +/// import event_hub /// -/// pub fn example(hub: observer.Hub(String)) { +/// pub fn example(hub: event_hub.Hub(String)) { /// let unsubscribe = -/// observer.subscribe(hub, fn(value) { +/// event_hub.subscribe(hub, fn(value) { /// io.println("Received value: " <> value) /// }) /// diff --git a/src/observer/filtered.gleam b/src/event_hub/filtered.gleam similarity index 90% rename from src/observer/filtered.gleam rename to src/event_hub/filtered.gleam index 0050b07..8c2d173 100644 --- a/src/observer/filtered.gleam +++ b/src/event_hub/filtered.gleam @@ -1,13 +1,13 @@ //// The `filtered` module provides a way to manage and notify subscribers about events based on specific topics. //// It supports creating observers that can filter events using topics, allowing more fine-grained control over event handling. -//// If you only want to use topics of the type `String`, you should use the `observer/topic` module instead. +//// If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead. //// //// ## Examples //// //// ### Filtered Observer //// ```gleam //// import gleam/io -//// import observer/filtered +//// import event_hub/filtered //// //// pub fn main() { //// use hub <- filtered.new() @@ -30,20 +30,20 @@ //// } //// ``` +import event_hub import gleam/list import gleam/set -import observer type TopicValuePair(value_type, topic_type) = #(List(topic_type), value_type) /// Represents a hub for managing event subscriptions and notifications based on topics. pub opaque type Hub(value_type, topic_type) { - Hub(hub: observer.Hub(TopicValuePair(value_type, topic_type))) + Hub(hub: event_hub.Hub(TopicValuePair(value_type, topic_type))) } -/// Creates a new filtered observer hub, executes the given context with the hub. -/// If you only want to use topics of the type `String`, you should use the `observer/topic` module instead. +/// Creates a new filtered event hub, executes the given context with the hub. +/// If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead. /// /// ## Parameters /// - `context`: A function that takes the created `Hub` and returns a result. @@ -53,7 +53,7 @@ pub opaque type Hub(value_type, topic_type) { /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example() { /// filtered.new(fn(hub) { @@ -63,7 +63,7 @@ pub opaque type Hub(value_type, topic_type) { /// } /// ``` pub fn new(in context: fn(Hub(value_type, topic_type)) -> result) -> result { - use hub <- observer.new() + use hub <- event_hub.new() let hub = Hub(hub) context(hub) @@ -79,7 +79,7 @@ pub fn new(in context: fn(Hub(value_type, topic_type)) -> result) -> result { /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub(String, String)) { /// filtered.notify(hub, ["topic1"], "event") @@ -90,7 +90,7 @@ pub fn notify( with topics: List(topic_type), and value: value_type, ) -> Nil { - observer.notify(hub.hub, #(topics, value)) + event_hub.notify(hub.hub, #(topics, value)) } /// Subscribes to specific topics and returns an unsubscribe function. @@ -107,7 +107,7 @@ pub fn notify( /// ## Example /// ```gleam /// import gleam/io -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub(String, String)) { /// let unsubscribe = @@ -122,11 +122,11 @@ pub fn notify( pub fn subscribe( on hub: Hub(value_type, topic_type), with topics: List(topic_type), - and callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + and callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { let topics = set.from_list(topics) - use #(event_topics, event_value) <- observer.subscribe(hub.hub) + use #(event_topics, event_value) <- event_hub.subscribe(hub.hub) case list.any(event_topics, fn(topic) { set.contains(topics, topic) }) { True -> callback(event_value) @@ -139,8 +139,8 @@ pub opaque type Hub2(value_type, topic_type1, topic_type2) { Hub2(hub: Hub(TopicValuePair(value_type, topic_type2), topic_type1)) } -/// Creates a new filtered observer hub with two levels of topics. -/// If you only want to use topics of the type `String`, you should use the `observer/topic` module instead. +/// Creates a new filtered event hub with two levels of topics. +/// If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead. /// /// ## Parameters /// - `context`: A function that takes the created `Hub2` and returns a result. @@ -150,7 +150,7 @@ pub opaque type Hub2(value_type, topic_type1, topic_type2) { /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example() { /// filtered.new2(fn(hub) { @@ -178,7 +178,7 @@ pub fn new2( /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub2(String, String, String)) { /// filtered.notify2(hub, ["topic1"], ["subtopic1"], "event") @@ -207,7 +207,7 @@ pub fn notify2( /// ## Example /// ```gleam /// import gleam/io -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub2(String, String, String)) { /// let unsubscribe = @@ -223,8 +223,8 @@ pub fn subscribe2( hub: Hub2(value_type, topic_type1, topic_type2), topics1: List(topic_type1), topics2: List(topic_type2), - callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { let topics = set.from_list(topics2) use #(event_topics, event_value) <- subscribe(hub.hub, topics1) @@ -242,8 +242,8 @@ pub opaque type Hub3(value_type, topic_type1, topic_type2, topic_type3) { ) } -/// Creates a new filtered observer hub with three levels of topics. -/// If you only want to use topics of the type `String`, you should use the `observer/topic` module instead. +/// Creates a new filtered event hub with three levels of topics. +/// If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead. /// /// ## Parameters /// - `context`: A function that takes the created `Hub3` and returns a result. @@ -253,7 +253,7 @@ pub opaque type Hub3(value_type, topic_type1, topic_type2, topic_type3) { /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example() { /// filtered.new3(fn(hub) { @@ -283,7 +283,7 @@ pub fn new3( /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub3(String, String, String, String)) { /// filtered.notify3(hub, ["topic1"], ["subtopic1"], ["subsubtopic1"], "event") @@ -314,7 +314,7 @@ pub fn notify3( /// ## Example /// ```gleam /// import gleam/io -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub3(String, String, String, String)) { /// let unsubscribe = @@ -335,8 +335,8 @@ pub fn subscribe3( topics1: List(topic_type1), topics2: List(topic_type2), topics3: List(topic_type3), - callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { let topics = set.from_list(topics3) use #(event_topics, event_value) <- subscribe2(hub.hub, topics1, topics2) @@ -365,8 +365,8 @@ pub opaque type Hub4( ) } -/// Creates a new filtered observer hub with four levels of topics. -/// If you only want to use topics of the type `String`, you should use the `observer/topic` module instead. +/// Creates a new filtered event hub with four levels of topics. +/// If you only want to use topics of the type `String`, you should use the `event_hub/topic` module instead. /// /// ## Parameters /// - `context`: A function that takes the created `Hub4` and returns a result. @@ -376,7 +376,7 @@ pub opaque type Hub4( /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example() { /// filtered.new4(fn(hub) { @@ -409,7 +409,7 @@ pub fn new4( /// /// ## Example /// ```gleam -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub4(String, String, String, String, String)) { /// filtered.notify4( @@ -449,7 +449,7 @@ pub fn notify4( /// ## Example /// ```gleam /// import gleam/io -/// import observer/filtered +/// import event_hub/filtered /// /// pub fn example(hub: filtered.Hub4(String, String, String, String, String)) { /// let unsubscribe = @@ -472,8 +472,8 @@ pub fn subscribe4( topics2: List(topic_type2), topics3: List(topic_type3), topics4: List(topic_type4), - callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { let topics = set.from_list(topics4) use #(event_topics, event_value) <- subscribe3( diff --git a/src/observer/reactive.gleam b/src/event_hub/reactive.gleam similarity index 88% rename from src/observer/reactive.gleam rename to src/event_hub/reactive.gleam index 92237f7..7182913 100644 --- a/src/observer/reactive.gleam +++ b/src/event_hub/reactive.gleam @@ -7,7 +7,7 @@ //// ### Reactive Observer //// ```gleam //// import gleam/io -//// import observer/reactive +//// import event_hub/reactive //// //// pub fn main() { //// let get_time = fn() { "2024-05-21T16:30:00Z" } @@ -23,7 +23,7 @@ //// } //// ``` -import observer +import event_hub /// Represents a function that returns the current state value. pub type State(value_type) = @@ -31,7 +31,7 @@ pub type State(value_type) = /// Represents a hub for managing event subscriptions and notifications based on dynamic state. pub opaque type Hub(value_type) { - Hub(inner: observer.Hub(value_type), state: State(value_type)) + Hub(inner: event_hub.Hub(value_type), state: State(value_type)) } /// Creates a new reactive observer hub, executes the given context with the hub. @@ -45,7 +45,7 @@ pub opaque type Hub(value_type) { /// /// ## Example /// ```gleam -/// import observer/reactive +/// import event_hub/reactive /// /// pub fn example() { /// let get_state = fn() { "current state" } @@ -60,7 +60,7 @@ pub fn new( with state: State(value_type), in context: fn(Hub(value_type)) -> result, ) -> result { - use inner <- observer.new() + use inner <- event_hub.new() let hub = Hub(inner, state) context(hub) @@ -77,7 +77,7 @@ pub fn new( /// /// ## Example /// ```gleam -/// import observer/reactive +/// import event_hub/reactive /// /// pub fn example(hub: reactive.Hub(String)) { /// let value = reactive.notify(hub) @@ -85,7 +85,7 @@ pub fn new( /// ``` pub fn notify(on hub: Hub(value_type)) -> value_type { let value = hub.state() - observer.notify(hub.inner, value) + event_hub.notify(hub.inner, value) value } @@ -102,7 +102,7 @@ pub fn notify(on hub: Hub(value_type)) -> value_type { /// ## Example /// ```gleam /// import gleam/io -/// import observer/reactive +/// import event_hub/reactive /// /// pub fn example(hub: reactive.Hub(String)) { /// let unsubscribe = @@ -116,7 +116,7 @@ pub fn notify(on hub: Hub(value_type)) -> value_type { /// ``` pub fn subscribe( on hub: Hub(value_type), - with callback: observer.Callback(value_type), -) -> observer.Unsubscribe { - observer.subscribe(hub.inner, callback) + with callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { + event_hub.subscribe(hub.inner, callback) } diff --git a/src/observer/stateful.gleam b/src/event_hub/stateful.gleam similarity index 82% rename from src/observer/stateful.gleam rename to src/event_hub/stateful.gleam index fe0e442..d6f93db 100644 --- a/src/observer/stateful.gleam +++ b/src/event_hub/stateful.gleam @@ -7,7 +7,7 @@ //// ### Stateful Observer //// ```gleam //// import gleam/io -//// import observer/stateful +//// import event_hub/stateful //// //// pub fn main() { //// use hub <- stateful.new("initial state") @@ -25,39 +25,39 @@ //// } //// ``` -import observer +import event_hub /// Starts the stateful observer process with an initial state. -@external(erlang, "observer_ffi", "start_stateful") -@external(javascript, "../observer_ffi.mjs", "startStateful") +@external(erlang, "event_hub_ffi", "start_stateful") +@external(javascript, "../event_hub_ffi.mjs", "startStateful") fn start_stateful(value: value_type) -> Hub(value_type) /// Adds a callback to the stateful observer, returning the current state and index. -@external(erlang, "observer_ffi", "add_stateful") -@external(javascript, "../observer_ffi.mjs", "addStateful") +@external(erlang, "event_hub_ffi", "add_stateful") +@external(javascript, "../event_hub_ffi.mjs", "addStateful") fn add_stateful( hub: Hub(value_type), - callback: observer.Callback(value_type), + callback: event_hub.Callback(value_type), ) -> #(value_type, Int) /// Retrieves the current state. -@external(erlang, "observer_ffi", "current_state") -@external(javascript, "../observer_ffi.mjs", "currentState") +@external(erlang, "event_hub_ffi", "current_state") +@external(javascript, "../event_hub_ffi.mjs", "currentState") fn current_state(hub: Hub(value_type)) -> value_type /// Invokes all callbacks in parallel with a new state, updating the state and waits for all callbacks to complete. -@external(erlang, "observer_ffi", "invoke_stateful") -@external(javascript, "../observer_ffi.mjs", "invokeStateful") +@external(erlang, "event_hub_ffi", "invoke_stateful") +@external(javascript, "../event_hub_ffi.mjs", "invokeStateful") fn invoke_stateful(hub: Hub(value_type), value: value_type) -> Nil /// Removes a callback by its index. -@external(erlang, "observer_ffi", "remove_stateful") -@external(javascript, "../observer_ffi.mjs", "removeStateful") +@external(erlang, "event_hub_ffi", "remove_stateful") +@external(javascript, "../event_hub_ffi.mjs", "removeStateful") fn remove_stateful(hub: Hub(value_type), index: Int) -> Nil /// Stops the stateful observer process. -@external(erlang, "observer_ffi", "stop_stateful") -@external(javascript, "../observer_ffi.mjs", "stopStateful") +@external(erlang, "event_hub_ffi", "stop_stateful") +@external(javascript, "../event_hub_ffi.mjs", "stopStateful") fn stop_stateful(hub: Hub(value_type)) -> Nil /// Represents a hub for managing event subscriptions and notifications with a mutable state. @@ -74,7 +74,7 @@ pub type Hub(value_type) /// /// ## Example /// ```gleam -/// import observer/stateful +/// import event_hub/stateful /// /// pub fn example() { /// stateful.new("initial state", fn(hub) { @@ -105,7 +105,7 @@ pub fn new( /// /// ## Example /// ```gleam -/// import observer/stateful +/// import event_hub/stateful /// /// pub fn example(hub: stateful.Hub(String)) { /// let current_state = stateful.state(hub) @@ -124,7 +124,7 @@ pub fn state(of hub: Hub(value_type)) -> value_type { /// /// ## Example /// ```gleam -/// import observer/stateful +/// import event_hub/stateful /// /// pub fn example(hub: stateful.Hub(String)) { /// stateful.notify(hub, "new state") @@ -149,7 +149,7 @@ pub fn notify(on hub: Hub(value_type), with value: value_type) -> Nil { /// ## Example /// ```gleam /// import gleam/io -/// import observer/stateful +/// import event_hub/stateful /// /// pub fn example(hub: stateful.Hub(String)) { /// let #(current_state, unsubscribe) = @@ -164,8 +164,8 @@ pub fn notify(on hub: Hub(value_type), with value: value_type) -> Nil { pub fn subscribe( on hub: Hub(value_type), should notify_current_state: Bool, - with callback: observer.Callback(value_type), -) -> #(value_type, observer.Unsubscribe) { + with callback: event_hub.Callback(value_type), +) -> #(value_type, event_hub.Unsubscribe) { case notify_current_state { True -> { let current_state = state(hub) diff --git a/src/observer/topic.gleam b/src/event_hub/topic.gleam similarity index 87% rename from src/observer/topic.gleam rename to src/event_hub/topic.gleam index a21ff9d..e916131 100644 --- a/src/observer/topic.gleam +++ b/src/event_hub/topic.gleam @@ -7,7 +7,7 @@ //// ### Single-Level Topic-Based Observer //// ```gleam //// import gleam/io -//// import observer/topic +//// import event_hub/topic //// //// pub fn main() { //// use hub <- topic.new() @@ -29,25 +29,25 @@ //// } //// ``` -import observer +import event_hub /// Starts the topic-based observer process. -@external(erlang, "observer_ffi", "start_topic_based") -@external(javascript, "../observer_ffi.mjs", "startTopicBased") +@external(erlang, "event_hub_ffi", "start_topic_based") +@external(javascript, "../event_hub_ffi.mjs", "startTopicBased") fn start_topic_based() -> HubN(value_type) /// Adds a callback with topics to the topic-based observer, returning the index. -@external(erlang, "observer_ffi", "add_topic_based") -@external(javascript, "../observer_ffi.mjs", "addTopicBased") +@external(erlang, "event_hub_ffi", "add_topic_based") +@external(javascript, "../event_hub_ffi.mjs", "addTopicBased") fn add_topic_based( hub: HubN(value_type), topics: List(List(String)), - callback: observer.Callback(value_type), + callback: event_hub.Callback(value_type), ) -> Int /// Invokes all matching callbacks in parallel with the given topics and value, and waits for all of them to complete. -@external(erlang, "observer_ffi", "invoke_topic_based") -@external(javascript, "../observer_ffi.mjs", "invokeTopicBased") +@external(erlang, "event_hub_ffi", "invoke_topic_based") +@external(javascript, "../event_hub_ffi.mjs", "invokeTopicBased") fn invoke_topic_based( hub: HubN(value_type), topics: List(List(String)), @@ -55,19 +55,19 @@ fn invoke_topic_based( ) -> Nil /// Removes a callback by its index. -@external(erlang, "observer_ffi", "remove_topic_based") -@external(javascript, "../observer_ffi.mjs", "removeTopicBased") +@external(erlang, "event_hub_ffi", "remove_topic_based") +@external(javascript, "../event_hub_ffi.mjs", "removeTopicBased") fn remove_topic_based(hub: HubN(value_type), index: Int) -> Nil /// Stops the topic-based observer process. -@external(erlang, "observer_ffi", "stop_topic_based") -@external(javascript, "../observer_ffi.mjs", "stopTopicBased") +@external(erlang, "event_hub_ffi", "stop_topic_based") +@external(javascript, "../event_hub_ffi.mjs", "stopTopicBased") fn stop_topic_based(hub: HubN(value_type)) -> Nil /// Represents a hub for managing event subscriptions and notifications based on hierarchical topics. pub type HubN(value_type) -/// Creates a new topic-based observer hub, executes the given context with the hub, and stops the hub afterward. +/// Creates a new topic-based event hub, executes the given context with the hub, and stops the hub afterward. /// /// ## Parameters /// - `context`: A function that takes the created `HubN` and returns a result. @@ -77,7 +77,7 @@ pub type HubN(value_type) /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example() { /// topic.new(fn(hub) { @@ -105,7 +105,7 @@ pub fn new_n(in context: fn(HubN(value_type)) -> result) -> result { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.HubN(String)) { /// topic.notify_n(hub, [["topic1"]], "event") @@ -133,7 +133,7 @@ pub fn notify_n( /// ## Example /// ```gleam /// import gleam/io -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.HubN(String)) { /// let unsubscribe = @@ -148,8 +148,8 @@ pub fn notify_n( pub fn subscribe_n( on hub: HubN(value_type), with topics: List(List(String)), - and callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + and callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { let index = add_topic_based(hub, topics, callback) fn() { remove_topic_based(hub, index) } } @@ -159,7 +159,7 @@ pub opaque type Hub(value_type) { Hub(hub: HubN(value_type)) } -/// Creates a new single-level topic-based observer hub, executes the given context with the hub. +/// Creates a new single-level topic-based event hub, executes the given context with the hub. /// /// ## Parameters /// - `context`: A function that takes the created `Hub` and returns a result. @@ -169,7 +169,7 @@ pub opaque type Hub(value_type) { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example() { /// topic.new(fn(hub) { @@ -192,7 +192,7 @@ pub fn new(in context: fn(Hub(value_type)) -> result) -> result { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.Hub(String)) { /// topic.notify(hub, ["topic1"], "event") @@ -219,7 +219,7 @@ pub fn notify( /// ## Example /// ```gleam /// import gleam/io -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.Hub(String)) { /// let unsubscribe = @@ -234,8 +234,8 @@ pub fn notify( pub fn subscribe( on hub: Hub(value_type), with topics: List(String), - and callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + and callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { subscribe_n(hub.hub, [topics], callback) } @@ -244,7 +244,7 @@ pub opaque type Hub2(value_type) { Hub2(hub: HubN(value_type)) } -/// Creates a new two-level topic-based observer hub, executes the given context with the hub. +/// Creates a new two-level topic-based event hub, executes the given context with the hub. /// /// ## Parameters /// - `context`: A function that takes the created `Hub2` and returns a result. @@ -254,7 +254,7 @@ pub opaque type Hub2(value_type) { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example() { /// topic.new2(fn(hub) { @@ -278,7 +278,7 @@ pub fn new2(in context: fn(Hub2(value_type)) -> result) -> result { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.Hub2(String)) { /// topic.notify2(hub, ["topic1"], ["subtopic1"], "event") @@ -306,7 +306,7 @@ pub fn notify2( /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// import gleam/io /// /// pub fn example(hub: topic.Hub2(String)) { @@ -323,8 +323,8 @@ pub fn subscribe2( hub: Hub2(value_type), topics1: List(String), topics2: List(String), - callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { subscribe_n(hub.hub, [topics1, topics2], callback) } @@ -333,7 +333,7 @@ pub opaque type Hub3(value_type) { Hub3(hub: HubN(value_type)) } -/// Creates a new three-level topic-based observer hub, executes the given context with the hub. +/// Creates a new three-level topic-based event hub, executes the given context with the hub. /// /// ## Parameters /// - `context`: A function that takes the created `Hub3` and returns a result. @@ -343,7 +343,7 @@ pub opaque type Hub3(value_type) { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example() { /// topic.new3(fn(hub) { @@ -368,7 +368,7 @@ pub fn new3(in context: fn(Hub3(value_type)) -> result) -> result { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.Hub3(String)) { /// topic.notify3(hub, ["topic1"], ["subtopic1"], ["subsubtopic1"], "event") @@ -399,7 +399,7 @@ pub fn notify3( /// ## Example /// ```gleam /// import gleam/io -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.Hub3(String)) { /// let unsubscribe = @@ -420,8 +420,8 @@ pub fn subscribe3( topics1: List(String), topics2: List(String), topics3: List(String), - callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { subscribe_n(hub.hub, [topics1, topics2, topics3], callback) } @@ -430,7 +430,7 @@ pub opaque type Hub4(value_type) { Hub4(hub: HubN(value_type)) } -/// Creates a new four-level topic-based observer hub, executes the given context with the hub. +/// Creates a new four-level topic-based event hub, executes the given context with the hub. /// /// ## Parameters /// - `context`: A function that takes the created `Hub4` and returns a result. @@ -440,7 +440,7 @@ pub opaque type Hub4(value_type) { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example() { /// topic.new4(fn(hub) { @@ -466,7 +466,7 @@ pub fn new4(in context: fn(Hub4(value_type)) -> result) -> result { /// /// ## Example /// ```gleam -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.Hub4(String)) { /// topic.notify4( @@ -506,7 +506,7 @@ pub fn notify4( /// ## Example /// ```gleam /// import gleam/io -/// import observer/topic +/// import event_hub/topic /// /// pub fn example(hub: topic.Hub4(String)) { /// let unsubscribe = @@ -529,7 +529,7 @@ pub fn subscribe4( topics2: List(String), topics3: List(String), topics4: List(String), - callback: observer.Callback(value_type), -) -> observer.Unsubscribe { + callback: event_hub.Callback(value_type), +) -> event_hub.Unsubscribe { subscribe_n(hub.hub, [topics1, topics2, topics3, topics4], callback) } diff --git a/src/observer_ffi.erl b/src/event_hub_ffi.erl similarity index 98% rename from src/observer_ffi.erl rename to src/event_hub_ffi.erl index ab13a2f..91d1796 100644 --- a/src/observer_ffi.erl +++ b/src/event_hub_ffi.erl @@ -1,4 +1,4 @@ --module(observer_ffi). +-module(event_hub_ffi). -export([ start_stateless/0, add_stateless/2, @@ -137,7 +137,7 @@ find_matching_callbacks(TopicIndex, TopicsList, Callbacks) -> start_stateless() -> spawn(fun() -> stateless_loop(#{}, 0) end). -%% The main loop for the stateless observer. +%% The main loop for the stateless event_hub. stateless_loop(Callbacks, Index) -> receive {add, Callback, From} -> @@ -185,7 +185,7 @@ stop_stateless(Process) -> start_stateful(State) -> spawn(fun() -> stateful_loop(State, #{}, 0) end). -%% The main loop for the stateful observer. +%% The main loop for the stateful event_hub. stateful_loop(State, Callbacks, Index) -> receive {add, Callback, From} -> @@ -243,7 +243,7 @@ stop_stateful(Process) -> start_topic_based() -> spawn(fun() -> topic_based_loop(#{}, #{}, 0) end). -%% The main loop for the topic-based observer. +%% The main loop for the topic-based event_hub. topic_based_loop(Callbacks, TopicIndex, Index) -> receive {add, Topics, Callback, From} -> diff --git a/src/observer_ffi.mjs b/src/event_hub_ffi.mjs similarity index 96% rename from src/observer_ffi.mjs rename to src/event_hub_ffi.mjs index a619822..a3a0a8e 100644 --- a/src/observer_ffi.mjs +++ b/src/event_hub_ffi.mjs @@ -157,7 +157,7 @@ function findMatchingCallbacks(topicIndex, topics, callbacks) { // ================== /** - * Creates a new stateless observer. + * Creates a new stateless event_hub. * @template T * @returns {Hub} */ @@ -202,7 +202,7 @@ export function removeStateless(hub, index) { } /** - * Clears all callbacks from the stateless observer. + * Clears all callbacks from the stateless event_hub. * @template T * @param {Hub} hub */ @@ -273,7 +273,7 @@ export function removeStateful(hub, index) { } /** - * Clears all callbacks and the value from the stateful observer. + * Clears all callbacks and the value from the stateful event_hub. * @template T * @param {StatefulHub} hub */ @@ -288,7 +288,7 @@ export function stopStateful(hub) { // ==================== /** - * Creates a new topic-based observer. + * Creates a new topic-based event_hub. * @template T * @returns {TopicBasedHub} */ @@ -353,7 +353,7 @@ export function removeTopicBased(hub, index) { } /** - * Clears all callbacks and the topic index from the stateful observer. + * Clears all callbacks and the topic index from the stateful event_hub. * @template T * @param {TopicBasedHub} hub */ diff --git a/src/observer_examples.gleam b/test/event_hub_examples.gleam similarity index 95% rename from src/observer_examples.gleam rename to test/event_hub_examples.gleam index 5a47465..248b154 100644 --- a/src/observer_examples.gleam +++ b/test/event_hub_examples.gleam @@ -1,12 +1,12 @@ +import event_hub +import event_hub/filtered +import event_hub/reactive +import event_hub/stateful +import event_hub/topic import gleam/int import gleam/io -import observer -import observer/filtered -import observer/reactive -import observer/stateful -import observer/topic -pub fn run_all_observer_examples() { +pub fn run_all_event_hub_examples() { io.println("Running examples...") run_example("Simple Observer", simple_observer) run_example("Simple Observer with State", simple_observer_with_state) @@ -27,7 +27,7 @@ fn run_example(name: String, example: fn() -> Nil) { } /// A simple observer implementation. -/// This example demonstrates the basic usage of the observer library. +/// This example demonstrates the basic usage of the Event-Hub library. /// It is an easy way to use publishers and subscribers to handle events. /// /// Outputs the following: @@ -38,15 +38,15 @@ fn run_example(name: String, example: fn() -> Nil) { /// ``` fn simple_observer() { // Creates a new hub for distributing events. - use hub <- observer.new() + use hub <- event_hub.new() // Notifies all subscribers of the hub that an event has occurred. - observer.notify(hub, 1) + event_hub.notify(hub, 1) // You can forward the hub to other functions or components. { // Using syntactic sugar for handling the callback. - use value <- observer.subscribe(hub) + use value <- event_hub.subscribe(hub) // This function gets called when the hub receives an event. io.println("[1] | Received an event with value: " <> int.to_string(value)) @@ -55,23 +55,23 @@ fn simple_observer() { // You can also subscribe using a normal callback function. // This also returns an unsubscribe function. let unsubscribe = - observer.subscribe(hub, fn(value) { + event_hub.subscribe(hub, fn(value) { io.println("[2] | Received an event with value: " <> int.to_string(value)) }) // Notifies all subscribers of the hub that an event has occurred with the value `2`. // These notifications occur in parallel but notify waits for all of them to complete. - observer.notify(hub, 2) + event_hub.notify(hub, 2) // Unsubscribe if you no longer need to receive events. unsubscribe() // Notify again to demonstrate that the unsubscribe function works. - observer.notify(hub, 3) + event_hub.notify(hub, 3) } /// A simple stateful observer implementation. -/// This is like the previous example, but it uses a stateful observer. +/// This is like the previous example, but it uses a stateful event_hub. /// /// Outputs the following: /// ```text @@ -283,7 +283,7 @@ fn multiple_topics() { unsubscribe_user_details() } -/// This example demonstrates the usage of the filtered observer. +/// This example demonstrates the usage of the filtered event_hub. /// It is an easy way to filter events based on a list of topics. /// They work in a similar way, but you can use generics to filter by any type. /// diff --git a/test/observer_test.gleam b/test/event_hub_test.gleam similarity index 96% rename from test/observer_test.gleam rename to test/event_hub_test.gleam index d94c201..809a37c 100644 --- a/test/observer_test.gleam +++ b/test/event_hub_test.gleam @@ -1,10 +1,10 @@ +import event_hub +import event_hub/filtered +import event_hub/reactive +import event_hub/stateful +import event_hub/topic import gleeunit import gleeunit/should -import observer -import observer/filtered -import observer/reactive -import observer/stateful -import observer/topic pub fn main() { gleeunit.main() @@ -63,32 +63,32 @@ pub fn stateless_test() { use a <- mut(0) use b <- mut(0) - use hub <- observer.new() + use hub <- event_hub.new() expect(a, 0) expect(b, 0) - observer.notify(hub, 1) + event_hub.notify(hub, 1) expect(a, 0) expect(b, 0) - let unsubscribe_a = observer.subscribe(hub, fn(value) { set(a, value) }) + let unsubscribe_a = event_hub.subscribe(hub, fn(value) { set(a, value) }) expect(a, 0) expect(b, 0) - observer.notify(hub, 2) + event_hub.notify(hub, 2) expect(a, 2) expect(b, 0) - let unsubscribe_b = observer.subscribe(hub, fn(value) { set(b, value) }) + let unsubscribe_b = event_hub.subscribe(hub, fn(value) { set(b, value) }) expect(a, 2) expect(b, 0) - observer.notify(hub, 3) + event_hub.notify(hub, 3) expect(a, 3) expect(b, 3) @@ -98,7 +98,7 @@ pub fn stateless_test() { expect(a, 3) expect(b, 3) - observer.notify(hub, 4) + event_hub.notify(hub, 4) expect(a, 3) expect(b, 4) @@ -108,7 +108,7 @@ pub fn stateless_test() { expect(a, 3) expect(b, 4) - observer.notify(hub, 5) + event_hub.notify(hub, 5) expect(a, 3) expect(b, 4) @@ -762,14 +762,14 @@ pub fn stateful_function_test() { pub fn stateless_hub_in_hub_test() { use a <- mut(0) - use inner_hub <- observer.new() - use outer_hub <- observer.new() + use inner_hub <- event_hub.new() + use outer_hub <- event_hub.new() - observer.subscribe(inner_hub, fn(value) { set(a, value) }) + event_hub.subscribe(inner_hub, fn(value) { set(a, value) }) - observer.subscribe(outer_hub, fn(hub) { observer.notify(hub, 1) }) + event_hub.subscribe(outer_hub, fn(hub) { event_hub.notify(hub, 1) }) - observer.notify(outer_hub, inner_hub) + event_hub.notify(outer_hub, inner_hub) expect(a, 1)