Module: FlatKit::EventEmitter

Included in:
Merge, Stats
Defined in:
lib/flat_kit/event_emitter.rb

Overview

Internal: A simplified Observable class for use internally

Instance Method Summary collapse

Instance Method Details

#_listenersObject



32
33
34
# File 'lib/flat_kit/event_emitter.rb', line 32

def _listeners
  @_listeners ||= []
end

#add_listener(listener) ⇒ Object

Raises:

  • (::NoMethodError)


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_listenersObject



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: 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_listenersObject



22
23
24
# File 'lib/flat_kit/event_emitter.rb', line 22

def remove_listeners
  _listeners.clear
end