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

#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|
    :delete == cb.call(*args, &block) # rubocop:disable Style/YodaCondition
  end
end

#on(event, &block) ⇒ Object

Subscribe to all future events for specified type.

Parameters:

  • event (Symbol)
  • block (Proc)

    callback function

Raises:

  • (ArgumentError)


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

def on(event, &block)
  raise ArgumentError, "must provide callback" unless block

  listeners(event.to_sym).push block
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)
  on(event) do |*args, &callback|
    block.call(*args, &callback)
    :delete
  end
end