Class: EventDispatcher::Dispatcher
- Inherits:
-
Object
- Object
- EventDispatcher::Dispatcher
- Defined in:
- lib/event_dispatcher/dispatcher.rb
Defined Under Namespace
Classes: Listener
Instance Attribute Summary collapse
-
#listeners ⇒ Object
readonly
Returns the value of attribute listeners.
Instance Method Summary collapse
-
#add_listener(event_name, listener, priority = 0) ⇒ Object
Connects a listener to the dispatcher so that it can be notified when an event is dispatched.
-
#dispatch(event_name, event) ⇒ Object
Notifies all listeners of the given event.
-
#has_listeners?(event_name) ⇒ Boolean
Checks if the given event has some listeners that want to listen to.
-
#initialize ⇒ Dispatcher
constructor
A new instance of Dispatcher.
-
#remove_listener!(event_name, listener) ⇒ Object
Removes an event listener from the specified event.
-
#remove_listeners!(event_name = nil) ⇒ Object
Removes a collection of event listeners attached to a specified event, or reset the entire container of listeners if no event is given.
Constructor Details
#initialize ⇒ Dispatcher
Returns a new instance of Dispatcher.
5 6 7 |
# File 'lib/event_dispatcher/dispatcher.rb', line 5 def initialize @listeners = {} end |
Instance Attribute Details
#listeners ⇒ Object (readonly)
Returns the value of attribute listeners.
3 4 5 |
# File 'lib/event_dispatcher/dispatcher.rb', line 3 def listeners @listeners end |
Instance Method Details
#add_listener(event_name, listener, priority = 0) ⇒ Object
Connects a listener to the dispatcher so that it can be notified when an event is dispatched.
19 20 21 22 23 24 25 26 |
# File 'lib/event_dispatcher/dispatcher.rb', line 19 def add_listener(event_name, listener, priority = 0) raise ArgumentError.new("Priority must be a Fixnum") unless priority.is_a?(Fixnum) event_name = symbolize_key(event_name) @listeners[event_name] ||= [] @listeners[event_name] << Listener.new(listener, priority) sort_listeners!(event_name) end |
#dispatch(event_name, event) ⇒ Object
Notifies all listeners of the given event. The event instance is then passed to each listener of that event.
48 49 50 51 52 53 54 55 56 |
# File 'lib/event_dispatcher/dispatcher.rb', line 48 def dispatch(event_name, event) event_name = symbolize_key(event_name) return unless @listeners.key?(event_name) && !event.nil? @listeners[event_name].each do |l| invoke_callable(l.listener, event) # A Listener is able to tell the dispatcher to stop all propagation of the event to future listeners. break if event.respond_to?(:stop_propagation) && event.stop_propagation end end |
#has_listeners?(event_name) ⇒ Boolean
Checks if the given event has some listeners that want to listen to.
10 11 12 13 |
# File 'lib/event_dispatcher/dispatcher.rb', line 10 def has_listeners?(event_name) event_name = symbolize_key(event_name) @listeners.key?(event_name) && @listeners[event_name].size > 0 ? true : false end |
#remove_listener!(event_name, listener) ⇒ Object
Removes an event listener from the specified event.
29 30 31 32 33 34 35 |
# File 'lib/event_dispatcher/dispatcher.rb', line 29 def remove_listener!(event_name, listener) event_name = symbolize_key(event_name) return unless @listeners.key?(event_name) && !listener.nil? @listeners[event_name].delete_if { |l| l.listener.eql?(listener) } sort_listeners!(event_name) end |
#remove_listeners!(event_name = nil) ⇒ Object
Removes a collection of event listeners attached to a specified event, or reset the entire container of listeners if no event is given.
38 39 40 41 42 43 44 45 |
# File 'lib/event_dispatcher/dispatcher.rb', line 38 def remove_listeners!(event_name = nil) if event_name.nil? @listeners.clear else event_name = symbolize_key(event_name) @listeners.delete(event_name) if @listeners.key?(event_name) end end |