module HTTP2Next::Emitter

Basic event emitter implementation with support for persistent and one-time event callbacks.

Public Instance Methods

emit(event, *args, &block) click to toggle source

Emit event with provided arguments.

@param event [Symbol] @param args [Array] arguments to be passed to the callbacks @param block [Proc] callback function

# File lib/http/2/next/emitter.rb, line 34
def emit(event, *args, &block)
  listeners(event).delete_if do |cb|
    :delete == cb.call(*args, &block) # rubocop:disable Style/YodaCondition
  end
end
on(event, &block) click to toggle source

Subscribe to all future events for specified type.

@param event [Symbol] @param block [Proc] callback function

# File lib/http/2/next/emitter.rb, line 12
def on(event, &block)
  raise ArgumentError, "must provide callback" unless block_given?

  listeners(event.to_sym).push block
end
once(event, &block) click to toggle source

Subscribe to next event (at most once) for specified type.

@param event [Symbol] @param block [Proc] callback function

# File lib/http/2/next/emitter.rb, line 22
def once(event, &block)
  on(event) do |*args, &callback|
    block.call(*args, &callback)
    :delete
  end
end

Private Instance Methods

listeners(event) click to toggle source
# File lib/http/2/next/emitter.rb, line 42
def listeners(event)
  @listeners ||= Hash.new { |hash, key| hash[key] = [] }
  @listeners[event]
end