Module: FlatKit::EventEmitter
Overview
Internal: A simplified Observable class for use internally
Instance Method Summary collapse
- #_listeners ⇒ Object
- #add_listener(listener) ⇒ Object
- #count_listeners ⇒ Object
- #notify_listeners(name:, data:, meta: nil) ⇒ Object
- #remove_listener(listener) ⇒ Object
- #remove_listeners ⇒ Object
Instance Method Details
#_listeners ⇒ Object
32 33 34 |
# File 'lib/flat_kit/event_emitter.rb', line 32 def _listeners @_listeners ||= [] end |
#add_listener(listener) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/flat_kit/event_emitter.rb', line 7 def add_listener(listener) raise ::NoMethodError, "#{listener} does not resond to #on_event" unless listener.respond_to?(:on_event) self._listeners ||= [] self._listeners << listener unless _listeners.include?(listener) end |
#count_listeners ⇒ Object
14 15 16 |
# File 'lib/flat_kit/event_emitter.rb', line 14 def count_listeners _listeners.size end |
#notify_listeners(name:, data:, meta: nil) ⇒ Object
26 27 28 29 30 |
# File 'lib/flat_kit/event_emitter.rb', line 26 def notify_listeners(name:, data:, meta: nil) _listeners.each do |l| l.on_event(name: name, data: data, meta: ) end end |
#remove_listener(listener) ⇒ Object
18 19 20 |
# File 'lib/flat_kit/event_emitter.rb', line 18 def remove_listener(listener) _listeners.delete(listener) end |
#remove_listeners ⇒ Object
22 23 24 |
# File 'lib/flat_kit/event_emitter.rb', line 22 def remove_listeners _listeners.clear end |