Class: Rixie::EventListener

Inherits:
Object
  • Object
show all
Defined in:
lib/rixie/event_listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_id: nil, task_id: nil) ⇒ EventListener

Returns a new instance of EventListener.



7
8
9
10
11
12
13
# File 'lib/rixie/event_listener.rb', line 7

def initialize(session_id: nil, task_id: nil)
  @session_id = session_id
  @task_id = task_id
  @run_id = nil
  @sequence_number = 0
  @listeners = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#run_id=(value) ⇒ Object (writeonly)

Sets the attribute run_id

Parameters:

  • value

    the value to set the attribute run_id to.



15
16
17
# File 'lib/rixie/event_listener.rb', line 15

def run_id=(value)
  @run_id = value
end

Instance Method Details

#emit(event) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rixie/event_listener.rb', line 22

def emit(event)
  @sequence_number += 1
  envelope = Event::Envelope.new(
    event: event,
    occurred_at: Time.now,
    session_id: @session_id,
    task_id: @task_id,
    run_id: @run_id,
    sequence_number: @sequence_number,
    event_id: SecureRandom.uuid
  )
  @listeners[event.class].each { |block| block.call(envelope) }
end

#on(event_class, &block) ⇒ Object



17
18
19
20
# File 'lib/rixie/event_listener.rb', line 17

def on(event_class, &block)
  @listeners[event_class] << block
  self
end