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
-
#add_listener(event, &block) ⇒ Object
(also: #on)
Subscribe to all future events for specified type.
-
#emit(event, *args, &block) ⇒ Object
Emit event with provided arguments.
-
#once(event, &block) ⇒ Object
Subscribe to next event (at most once) for specified type.
Instance Method Details
#add_listener(event, &block) ⇒ Object Also known as: on
Subscribe to all future events for specified type.
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.
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.
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 |