class Faulty::Events::CallbackListener

A simple listener implementation that uses callback blocks as handlers

Each event in {EVENTS} has a method on this class that can be used to register a callback for that event.

@example

listener = CallbackListener.new
listener.circuit_opened do |payload|
  logger.error(
    "Circuit #{payload[:circuit].name} opened: #{payload[:error].message}"
  )
end

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/faulty/events/callback_listener.rb, line 18
def initialize
  @handlers = {}
  yield self if block_given?
end

Public Instance Methods

handle(event, payload) click to toggle source

@param (see ListenerInterface#handle) @return [void]

# File lib/faulty/events/callback_listener.rb, line 25
def handle(event, payload)
  return unless EVENT_SET.include?(event)
  return unless @handlers.key?(event)

  @handlers[event].each do |handler|
    handler.call(payload)
  end
end