Class: Eventception::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/eventception/dispatcher.rb

Instance Method Summary collapse

Instance Method Details

#add_listener(event_name:, listener:, listener_method:, priority: 0) ⇒ Object

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


137
138
139
140
141
142
# 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:) ⇒ Object

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


184
185
186
187
188
189
190
191
192
193
194
195
# 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) ⇒ Object

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.


60
61
62
63
64
65
66
# 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

#do_dispatch(listeners:, event:) ⇒ Object (protected)

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


235
236
237
238
239
240
241
242
243
244
245
# 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

#listenersObject

Gets all listeners sorted by descending priority.

Returns:

All event listeners sorted by event_name and descending priority.


73
74
75
76
77
78
79
80
81
# 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?Boolean

Checks whether are any registered listeners.

Returns:

Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/eventception/dispatcher.rb', line 88

def listeners?
  listeners.any?
end

#listeners_for(event_name:) ⇒ Object

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.


101
102
103
104
105
106
107
# 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:) ⇒ Boolean

Checks whether are any registered listeners for the specific event.

Parameters:

event_name

The name of the event

Returns:

Boolean

Returns:

  • (Boolean)


118
119
120
# 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:) ⇒ Object

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


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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:) ⇒ Object

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


208
209
210
211
212
213
214
215
216
217
218
# 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