Module: HTTP2::Emitter

Included in:
Connection, Stream
Defined in:
lib/http/2/emitter.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#add_listener(event, &block) ⇒ Object Also known as: on

Subscribe to all future events for specified type.

Parameters:

  • event (Symbol)
  • block (Proc)

    callback function

Raises:

  • (Exception)


12
13
14
15
# File 'lib/http/2/emitter.rb', line 12

def add_listener(event, &block)
  raise Exception.new("must provide callback") if !block_given?
  listeners(event.to_sym).push block
end

#emit(event, *args, &block) ⇒ Object

Emit event with provided arguments.

Parameters:

  • event (Symbol)
  • args (Array)

    arguments to be passed to the callbacks

  • block (Proc)

    callback function



34
35
36
37
38
# File 'lib/http/2/emitter.rb', line 34

def emit(event, *args, &block)
  listeners(event).delete_if do |cb|
    cb.call(*args, &block) == :delete
  end
end

#once(event, &block) ⇒ Object

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

Parameters:

  • event (Symbol)
  • block (Proc)

    callback function



22
23
24
25
26
27
# File 'lib/http/2/emitter.rb', line 22

def once(event, &block)
  add_listener(event) do |*args, &callback|
    block.call(*args, &callback)
    :delete
  end
end