class Eventception::Dispatcher

Public Instance Methods

add_listener(event_name:, listener:, listener_method:, priority: 0) click to toggle source

Add an event listener that listens to the specified event.

Parameters:

event_name

The event to listen on

listener

The listener

listener_method

The name of the method to be executed in the listener

priority

The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)

Returns:

Nil
# File lib/eventception/dispatcher.rb, line 137
def add_listener(event_name:, listener:, listener_method:, priority: 0)
  event_listeners[event_name][priority] << ListenerHandler.new(listener, listener_method)
  sorted.delete(event_name)

  nil
end
add_subscriber(subscriber:) click to toggle source

Add an event subscriber.

The subscriber is asked for all the events he is interested in and added as a listener for these events.

Parameters:

subscriber

The subscriber

Returns:

Nil
# File lib/eventception/dispatcher.rb, line 184
def add_subscriber(subscriber:)
  subscriber.subscribed_events.each do |event_subscribed|
    add_listener(
      event_name: event_subscribed.fetch(:event_name),
      listener: subscriber,
      listener_method: event_subscribed.fetch(:listener_method),
      priority: event_subscribed[:priority] || 0,
    )
  end

  nil
end
dispatch(event_name:, event: Eventception::Event.new) click to toggle source

Dispatches an event to all registered listeners.

Parameters:

event_name

The name of the event to dispatch. The name of the event is the name of the method that is invoked on listeners.

event

The event to pass to the event handlers/listeners If not supplied, an empty Event instance is created.

Returns:

The Event.
# File lib/eventception/dispatcher.rb, line 60
def dispatch(event_name:, event: Eventception::Event.new)
  if listeners_for?(event_name: event_name)
    do_dispatch(listeners: listeners_for(event_name: event_name), event: event)
  end

  event
end
listeners() click to toggle source

Gets all listeners sorted by descending priority.

Returns:

All event listeners sorted by event_name and descending priority.
# File lib/eventception/dispatcher.rb, line 73
def listeners
  return [] if event_listeners.empty?

  event_listeners.each_key do |event_name|
    sort_listeners(event_name) if sorted[event_name].empty?
  end

  sorted
end
listeners?() click to toggle source

Checks whether are any registered listeners.

Returns:

Boolean
# File lib/eventception/dispatcher.rb, line 88
def listeners?
  listeners.any?
end
listeners_for(event_name:) click to toggle source

Gets all listeners for the specific event sorted by descending priority.

Parameters:

event_name

The name of the event

Returns:

The event listeners for the specific event sorted by descending priority.
# File lib/eventception/dispatcher.rb, line 101
def listeners_for(event_name:)
  return [] if event_listeners[event_name].empty?

  sort_listeners(event_name) if sorted[event_name].empty?

  sorted[event_name]
end
listeners_for?(event_name:) click to toggle source

Checks whether are any registered listeners for the specific event.

Parameters:

event_name

The name of the event

Returns:

Boolean
# File lib/eventception/dispatcher.rb, line 118
def listeners_for?(event_name:)
  event_listeners[event_name].any?
end
remove_listener(event_name:, listener:, listener_method:) click to toggle source

Removes an event listener from the specified events.

Parameters:

event_name

The event to listen on

listener

The listener

listener_method

The name of the method to be executed in the listener

Returns:

Nil
# File lib/eventception/dispatcher.rb, line 157
def remove_listener(event_name:, listener:, listener_method:)
  return unless listeners_for?(event_name: event_name)

  listener_for_event = event_listeners.fetch(event_name)

  listener_for_event.each do |priority, priority_listeners|
    sorted.delete(event_name) if priority_listeners.delete(ListenerHandler.new(listener, listener_method))

    listener_for_event.delete(priority) if priority_listeners.empty?
  end

  event_listeners.delete(event_name) if listener_for_event.empty?

  nil
end
remove_subscriber(subscriber:) click to toggle source

Removes an event subscriber.

The subscriber is asked for all the events he is interested in and added as a listener for these events.

Parameters:

subscriber

The subscriber

Returns:

Nil
# File lib/eventception/dispatcher.rb, line 208
def remove_subscriber(subscriber:)
  subscriber.subscribed_events.each do |event_subscribed|
    remove_listener(
      event_name: event_subscribed.fetch(:event_name),
      listener: subscriber,
      listener_method: event_subscribed.fetch(:listener_method),
    )
  end

  nil
end

Protected Instance Methods

do_dispatch(listeners:, event:) click to toggle source

Triggers the listeners of an event.

This method can be overridden to add functionality that is executed for each listener.

Parameters:

listeners

The event listeners

event

The event

Returns:

Nil
# File lib/eventception/dispatcher.rb, line 235
def do_dispatch(listeners:, event:)
  listeners.each do |_priority, priority_listeners|
    priority_listeners.each do |listener_handler|
      return nil if event.propagation_stopped?

      listener_handler.call(event)
    end
  end

  nil
end

Private Instance Methods

event_listeners() click to toggle source
# File lib/eventception/dispatcher.rb, line 30
def event_listeners
  @event_listeners ||= Hash.new { |hash, key|
    priority_listeners_hash = Hash.new { |priority_hash, priority_level|
      priority_hash[priority_level] = PriorityListeners.new(priority: priority_level)
    }

    hash[key] = priority_listeners_hash
  }
end
sort_listeners(event_name) click to toggle source

Sorts the internal list of listeners for the given event by priority.

Parameters:

event_name

The event name

Returns:

Nil
# File lib/eventception/dispatcher.rb, line 258
def sort_listeners(event_name)
  sorted[event_name] = event_listeners[event_name].sort_by { |key, _| -key }.to_h

  nil
end
sorted() click to toggle source
# File lib/eventception/dispatcher.rb, line 40
def sorted
  @sorted ||= Hash.new { |hash, key| hash[key] = {} }
end