EventBus (event_bus v1.7.0) View Source

Traceable, extendable and minimalist event bus implementation for Elixir with built-in event store and event observation manager based on ETS.

Link to this section Summary

Types

EventBus.Model.Event struct

Event id

Tuple of topic name and event id

Event subscriber

Subscriber configuration

Event subscriber with config

Tuple of subscriber and event reference

Tuple of subscriber and event shadow

Tuple of subscriber, topic and event id

Tuple of subscriber and list of topic patterns

Event subscriber without config

List of event subscribers

Topic name

Regex pattern to match topic name

List of topic patterns

List of topic names

Functions

Fetch an event.

Fetch an event's data.

Mark the event as completed for the subscriber.

Mark the event as skipped for the subscriber.

Send an event to all subscribers.

Register a topic.

Subscribe a subscriber to the event bus.

Check if the given subscriber subscribed to the event bus for the given topic patterns.

List the subscribers.

List the subscribers for the given topic.

Check if a topic registered.

List all the registered topics.

Unregister a topic.

Unsubscribe a subscriber from the event bus.

Link to this section Types

Specs

event() :: EventBus.Model.Event.t()

EventBus.Model.Event struct

Specs

event_id() :: String.t() | integer()

Event id

Specs

event_shadow() :: {topic(), event_id()}

Tuple of topic name and event id

Specs

Event subscriber

Specs

subscriber_config() :: any()

Subscriber configuration

Link to this type

subscriber_with_config()

View Source

Specs

subscriber_with_config() :: {module(), subscriber_config()}

Event subscriber with config

Link to this type

subscriber_with_event_ref()

View Source

Specs

Tuple of subscriber and event reference

Link to this type

subscriber_with_event_shadow()

View Source

Specs

subscriber_with_event_shadow() :: {subscriber(), event_shadow()}

Tuple of subscriber and event shadow

Link to this type

subscriber_with_topic_and_event_id()

View Source

Specs

subscriber_with_topic_and_event_id() :: {subscriber(), topic(), event_id()}

Tuple of subscriber, topic and event id

Link to this type

subscriber_with_topic_patterns()

View Source

Specs

subscriber_with_topic_patterns() :: {subscriber(), topic_patterns()}

Tuple of subscriber and list of topic patterns

Link to this type

subscriber_without_config()

View Source

Specs

subscriber_without_config() :: module()

Event subscriber without config

Specs

subscribers() :: [subscriber()]

List of event subscribers

Specs

topic() :: atom()

Topic name

Specs

topic_pattern() :: String.t()

Regex pattern to match topic name

Specs

topic_patterns() :: [topic_pattern()]

List of topic patterns

Specs

topics() :: [topic()]

List of topic names

Link to this section Functions

Link to this function

fetch_event(event_shadow)

View Source

Specs

fetch_event(event_shadow()) :: event() | nil

Fetch an event.

Examples

EventBus.fetch_event({:hello_received, "123"})
%EventBus.Model.Model{}
Link to this function

fetch_event_data(event_shadow)

View Source

Specs

fetch_event_data(event_shadow()) :: any()

Fetch an event's data.

Examples

EventBus.fetch_event_data({:hello_received, "123"})
Link to this function

mark_as_completed(subscriber_with_event_ref)

View Source

Specs

mark_as_completed(subscriber_with_event_ref()) :: :ok

Mark the event as completed for the subscriber.

Examples

topic        = :hello_received
event_id     = "124"
event_shadow = {topic, event_id}

# For regular subscribers
EventBus.mark_as_completed({MyEventSubscriber, event_shadow})

# For configurable subscribers you must pass tuple of subscriber and config
my_config = %{}
subscriber  = {OtherSubscriber, my_config}

EventBus.mark_as_completed({subscriber, event_shadow})
:ok
Link to this function

mark_as_skipped(subscriber_with_event_ref)

View Source

Specs

mark_as_skipped(subscriber_with_event_ref()) :: :ok

Mark the event as skipped for the subscriber.

Examples

EventBus.mark_as_skipped({MyEventSubscriber, {:unmatched_occurred, "124"}})

# For configurable subscribers you must pass tuple of subscriber and config
my_config = %{}
subscriber  = {OtherSubscriber, my_config}
EventBus.mark_as_skipped({subscriber, {:unmatched_occurred, "124"}})
:ok

Specs

notify(event()) :: :ok

Send an event to all subscribers.

Examples

event = %Event{id: 1, topic: :webhook_received,
  data: %{"message" => "Hi all!"}}
EventBus.notify(event)
:ok

Specs

register_topic(topic()) :: :ok

Register a topic.

Examples

EventBus.register_topic(:demo_topic)
:ok
Link to this function

subscribe(subscriber_with_topic_patterns)

View Source

Specs

subscribe(subscriber_with_topic_patterns()) :: :ok

Subscribe a subscriber to the event bus.

Examples

EventBus.subscribe({MyEventSubscriber, [".*"]})
:ok

# For configurable subscribers you can pass tuple of subscriber and config
my_config = %{}
EventBus.subscribe({{OtherSubscriber, my_config}, [".*"]})
:ok
Link to this function

subscribed?(subscriber_with_topic_patterns)

View Source

Specs

Check if the given subscriber subscribed to the event bus for the given topic patterns.

Examples

EventBus.subscribe({MyEventSubscriber, [".*"]})
:ok

EventBus.subscribed?({MyEventSubscriber, [".*"]})
true

EventBus.subscribed?({MyEventSubscriber, ["some_initialized"]})
false

EventBus.subscribed?({AnothEventSubscriber, [".*"]})
false

Specs

subscribers() :: subscribers()

List the subscribers.

Examples

EventBus.subscribers()
[MyEventSubscriber]

# One usual and one configured subscriber with its config
EventBus.subscribers()
[MyEventSubscriber, {OtherSubscriber, %{}}]

Specs

subscribers(topic()) :: subscribers()

List the subscribers for the given topic.

Examples

EventBus.subscribers(:metrics_received)
[MyEventSubscriber]

# One usual and one configured subscriber with its config
EventBus.subscribers(:metrics_received)
[MyEventSubscriber, {OtherSubscriber, %{}}]

Specs

topic_exist?(topic()) :: boolean()

Check if a topic registered.

Examples

EventBus.topic_exist?(:demo_topic)
true

Specs

topics() :: topics()

List all the registered topics.

Examples

EventBus.topics()
[:metrics_summed]

Specs

unregister_topic(topic()) :: :ok

Unregister a topic.

Examples

EventBus.unregister_topic(:demo_topic)
:ok

Specs

unsubscribe(subscriber()) :: :ok

Unsubscribe a subscriber from the event bus.

Examples

EventBus.unsubscribe(MyEventSubscriber)
:ok

# For configurable subscribers you must pass tuple of subscriber and config
my_config = %{}
EventBus.unsubscribe({OtherSubscriber, my_config})
:ok