module HTTP2::Emitter
Basic event emitter implementation with support for persistent and one-time event callbacks.
Public Instance Methods
add_listener(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/emitter.rb, line 10 def add_listener(event, &block) fail ArgumentError, 'must provide callback' unless block_given? listeners(event.to_sym).push block end
Also aliased as: on
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/emitter.rb, line 32 def emit(event, *args, &block) listeners(event).delete_if do |cb| cb.call(*args, &block) == :delete end 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/emitter.rb, line 20 def once(event, &block) add_listener(event) do |*args, &callback| block.call(*args, &callback) :delete end end
Private Instance Methods
listeners(event)
click to toggle source
# File lib/http/2/emitter.rb, line 40 def listeners(event) @listeners ||= Hash.new { |hash, key| hash[key] = [] } @listeners[event] end