Module: NewRelic::Security::EventEmitter::InstanceMethods
- Defined in:
- lib/newrelic_security/websocket-client-simple/event_emitter.rb
Instance Method Summary collapse
- #__events ⇒ Object
- #add_listener(type, params = {}, &block) ⇒ Object (also: #on)
- #emit(type, *data) ⇒ Object
- #once(type, &block) ⇒ Object
- #remove_listener(id_or_type) ⇒ Object
Instance Method Details
#__events ⇒ Object
16 17 18 |
# File 'lib/newrelic_security/websocket-client-simple/event_emitter.rb', line 16 def __events @__events ||= [] end |
#add_listener(type, params = {}, &block) ⇒ Object Also known as: on
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/newrelic_security/websocket-client-simple/event_emitter.rb', line 20 def add_listener(type, params={}, &block) raise ArgumentError, 'listener block not given' unless block_given? id = __events.empty? ? 0 : __events.last[:id]+1 __events << { :type => type.to_sym, :listener => block, :params => params, :id => id } id end |
#emit(type, *data) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/newrelic_security/websocket-client-simple/event_emitter.rb', line 46 def emit(type, *data) type = type.to_sym __events.each do |e| case e[:type] when type listener = e[:listener] e[:type] = nil if e[:params][:once] instance_exec(*data, &listener) when :* listener = e[:listener] e[:type] = nil if e[:params][:once] instance_exec(type, *data, &listener) end end __events.each do |e| remove_listener e[:id] unless e[:type] end end |
#once(type, &block) ⇒ Object
65 66 67 |
# File 'lib/newrelic_security/websocket-client-simple/event_emitter.rb', line 65 def once(type, &block) add_listener type, {:once => true}, &block end |
#remove_listener(id_or_type) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/newrelic_security/websocket-client-simple/event_emitter.rb', line 34 def remove_listener(id_or_type) if id_or_type.is_a? Integer __events.delete_if do |e| e[:id] == id_or_type end elsif [String, Symbol].include? id_or_type.class __events.delete_if do |e| e[:type] == id_or_type.to_sym end end end |