Class: Eventception::ImmutableDispatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(listeners: [], subscribers: []) ⇒ ImmutableDispatcher

Creates an unmodifiable proxy for an event dispatcher.

Parameters:

listeners

Array of event listeners

subscribers

Array of event subscribers

Returns:

Nil


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/eventception/immutable_dispatcher.rb', line 41

def initialize(listeners: [], subscribers: [])
  @dispatcher = Eventception::Dispatcher.new

  listeners.each do |listener|
    dispatcher.add_listener(
      event_name: listener.fetch(:event_name),
      listener: listener.fetch(:listener),
      listener_method: listener.fetch(:listener_method),
      priority: listener.fetch(:priority, 0),
    )
  end

  subscribers.each { |subscriber| dispatcher.add_subscriber(subscriber: subscriber) }

  nil
end

Instance Method Details

#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.


72
73
74
75
76
# File 'lib/eventception/immutable_dispatcher.rb', line 72

def dispatch(event_name:, event: Eventception::Event.new)
  dispatcher.dispatch(event_name: event_name, event: event)

  event
end

#listenersObject

Gets all listeners sorted by descending priority.

Returns:

All event listeners sorted by event_name and descending priority.


83
84
85
# File 'lib/eventception/immutable_dispatcher.rb', line 83

def listeners
  dispatcher.listeners
end

#listeners?Boolean

Checks whether are any registered listeners.

Returns:

Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/eventception/immutable_dispatcher.rb', line 92

def listeners?
  dispatcher.listeners?
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.


105
106
107
# File 'lib/eventception/immutable_dispatcher.rb', line 105

def listeners_for(event_name:)
  dispatcher.listeners_for(event_name: 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/immutable_dispatcher.rb', line 118

def listeners_for?(event_name:)
  dispatcher.listeners_for?(event_name: event_name)
end