class EventEmitter

Constants

VERSION

Attributes

events[R]

Public Class Methods

instance() click to toggle source
# File lib/async_event_emitter.rb, line 9
def instance
  @instance ||= new
end
new() click to toggle source
# File lib/async_event_emitter.rb, line 16
def initialize
  @events = default_hash
end

Public Instance Methods

notify(event, data = {}) click to toggle source
# File lib/async_event_emitter.rb, line 20
def notify(event, data = {})
  events[event.to_sym].each do |subscriber:, **options|
    if options[:async]
      subscriber.delay.public_send(options[:method], data)
    else
      subscriber.public_send(options[:method], data)
    end
  end
end
observe(event, subscriber, **options) click to toggle source
# File lib/async_event_emitter.rb, line 30
def observe(event, subscriber, **options)
  events[event.to_sym] << {
    subscriber: subscriber,
    method: options.fetch(:method, event),
    async: options.fetch(:async, true)
  }
end
reset() click to toggle source
# File lib/async_event_emitter.rb, line 38
def reset
  @events = default_hash
end

Private Instance Methods

default_hash() click to toggle source
# File lib/async_event_emitter.rb, line 44
def default_hash
  Hash.new { |hash, key| hash[key] = [] }
end