EventBus
Simple event bus implementation.
Installation
The package can be installed by adding event_bus to your list of dependencies in mix.exs:
def deps do
[{:event_bus, "~> 0.1.0"}]
end
Usage
Module docs can be found at https://hexdocs.pm/event_bus.
Subscribe to the 'event bus'
EventBus.subscribe(MyEventListener)
Unsubscribe from the 'event bus'
EventBus.unsubscribe(MyEventListener)
List subscribers
EventBus.subscribers()
Notify all subscribers with any type of data
EventBus.notify(:hello_received, %{message: "Hello"})
EventBus.notify(:bye_received, [user_id: 1, goal: "exit"])
Sample Listener Implementation
defmodule MyEventListener do
...
def process({:hello_received, _event_data} = event) do
GenServer.cast(__MODULE__, event)
end
def process({:bye_received, event_data}) do
# do sth
:ok
end
def process({event_type, event_data}) do
# this one matches all events
:ok
end
...
end
Todo
- Move event data to ETS and pass key instead of data to listeners
Contributing
Issues, Bugs, Documentation, Enhancements
Fork the project
Make your improvements and write your tests(make sure you covered all the cases).
Make a pull request.
License
MIT