Class: Faulty::Events::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/faulty/events/notifier.rb

Overview

The default event dispatcher for Faulty

Instance Method Summary collapse

Constructor Details

#initialize(listeners = []) ⇒ Notifier

Returns a new instance of Notifier.

Parameters:

  • listeners (Array<ListenerInterface>) (defaults to: [])

    An array of event listeners



8
9
10
# File 'lib/faulty/events/notifier.rb', line 8

def initialize(listeners = [])
  @listeners = listeners.freeze
end

Instance Method Details

#notify(event, payload) ⇒ Object

Notify all listeners of an event

If a listener raises an error while handling an event, that error will be captured and written to STDERR.

Parameters:

  • event (Symbol)

    The event name

  • payload (Hash)

    A hash of event payload data. The payload keys differ between events, but should be consistent across calls for a single event

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/faulty/events/notifier.rb', line 21

def notify(event, payload)
  raise ArgumentError, "Unknown event #{event}" unless EVENTS.include?(event)

  @listeners.each do |listener|
    begin
      listener.handle(event, payload)
    rescue StandardError => e
      warn "Faulty listener #{listener.class.name} crashed: #{e.message}"
    end
  end
end