Sha256: 685b39c1df64684ab81a4dacb58623e19ca212882c59ef1414b359758f4ec674

Contents?: true

Size: 1.81 KB

Versions: 20

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true
require 'proxy_rb/event_bus/name_resolver'
require 'proxy_rb/errors'

module ProxyRb
  # Event bus
  #
  # Implements and in-process pub-sub events broadcaster allowing multiple observers
  # to subscribe to different events that fire as your tests are executed.
  #
  class EventBus
    # Create EventBus
    #
    # @param [#transform] resolver
    #   A resolver which transforms Symbol, String, Class into an event Class.
    def initialize(resolver)
      @resolver = resolver
      @handlers = Hash.new { |h, k| h[k] = [] }
    end

    # Register for an event
    #
    # @param [String, Symbol, Class, Array] event_ids
    #   If Array, register multiple events witht the same handler. If String,
    #   Symbol, Class register handler for given event.
    #
    # @param [#call] handler_object
    #   The handler object, needs to have method `#call`. Either
    #   `handler_object` or `block` can be defined. The handler object gets the
    #   event passed to `#call`.
    #
    # @yield
    #   Handler block which gets the event passed as parameter.
    def register(event_ids, handler_object = nil, &handler_proc)
      handler = handler_proc || handler_object

      raise ArgumentError, 'Please pass either an object#call or a handler block' if handler.nil? || !handler.respond_to?(:call)

      Array(event_ids).flatten.each do |id|
        @handlers[
          @resolver.transform(id).to_s
        ] << handler
      end

      nil
    end

    # Broadcast an event
    #
    # @param [Object] event
    #   An object of registered event class. This object is passed to the event
    #   handler.
    #
    def notify(event)
      raise NoEventError, 'Please pass an event object, not a class' if event.is_a?(Class)

      @handlers[event.class.to_s].each { |handler| handler.call(event) }
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
proxy_rb-1.0.0 lib/proxy_rb/event_bus.rb
proxy_rb-0.10.6 lib/proxy_rb/event_bus.rb
proxy_rb-0.10.5 lib/proxy_rb/event_bus.rb
proxy_rb-0.10.4 lib/proxy_rb/event_bus.rb
proxy_rb-0.10.3 lib/proxy_rb/event_bus.rb
proxy_rb-0.10.2 lib/proxy_rb/event_bus.rb
proxy_rb-0.10.1 lib/proxy_rb/event_bus.rb
proxy_rb-0.10.0 lib/proxy_rb/event_bus.rb
proxy_rb-0.9.3 lib/proxy_rb/event_bus.rb
proxy_rb-0.9.2 lib/proxy_rb/event_bus.rb
proxy_rb-0.9.1 lib/proxy_rb/event_bus.rb
proxy_rb-0.9.0 lib/proxy_rb/event_bus.rb
proxy_rb-0.8.3 lib/proxy_rb/event_bus.rb
proxy_rb-0.8.2 lib/proxy_rb/event_bus.rb
proxy_rb-0.8.1 lib/proxy_rb/event_bus.rb
proxy_rb-0.8.0 lib/proxy_rb/event_bus.rb
proxy_rb-0.7.1 lib/proxy_rb/event_bus.rb
proxy_rb-0.7.0 lib/proxy_rb/event_bus.rb
proxy_rb-0.6.0 lib/proxy_rb/event_bus.rb
proxy_rb-0.5.0 lib/proxy_rb/event_bus.rb