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



10
11
12
13
# 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

#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



32
33
34
35
36
# 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) ⇒ Object

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

Parameters:

  • event (Symbol)
  • block (Proc)

    callback function



20
21
22
23
24
25
# 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