Class: Termfront::Mission::EventRuntime

Inherits:
Object
  • Object
show all
Defined in:
lib/termfront/mission/event_runtime.rb

Instance Method Summary collapse

Constructor Details

#initialize(events) ⇒ EventRuntime

Returns a new instance of EventRuntime.



8
9
10
11
# File 'lib/termfront/mission/event_runtime.rb', line 8

def initialize(events)
  @events = events
  @fired = Set.new
end

Instance Method Details

#trigger(type, payload = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/termfront/mission/event_runtime.rb', line 13

def trigger(type, payload = {})
  normalized_type = type.to_sym
  normalized_payload = payload.transform_keys(&:to_sym)

  @events.filter_map do |event|
    next if event[:once] && @fired.include?(event[:id])
    next unless matches?(event[:trigger], normalized_type, normalized_payload)

    @fired << event[:id] if event[:once]
    event
  end
end