Skip to content

Commit

Permalink
Rename to event_hub
Browse files Browse the repository at this point in the history
  • Loading branch information
yerTools committed May 22, 2024
1 parent de43c61 commit b54ec4e
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 223 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Changelog

## Version 1.0.0
## 22.05.2024 - Version 1.0.0

- Initial release
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <https://hexdocs.pm/observer>.
Further documentation can be found at <https://hexdocs.pm/event_hub>.

- [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
Expand All @@ -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:
Expand All @@ -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))
Expand All @@ -70,27 +70,27 @@ 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)
}
````

### Simple Observer with State

````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
Expand Down Expand Up @@ -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.
///
Expand Down
10 changes: 3 additions & 7 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -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" },
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand All @@ -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"
}
48 changes: 24 additions & 24 deletions src/observer.gleam → src/event_hub.gleam
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
//// 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
////
//// ### 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.
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
/// })
///
Expand Down
Loading

0 comments on commit b54ec4e

Please sign in to comment.