Module: HTTP2Next::Emitter
- Included in:
- Connection, Stream
- Defined in:
- lib/http/2/next/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.
12 13 14 15 16 |
# File 'lib/http/2/next/emitter.rb', line 12 def add_listener(event, &block) raise ArgumentError, "must provide callback" unless block_given? listeners(event.to_sym).push block end |
#emit(event, *args, &block) ⇒ Object
Emit event with provided arguments.
35 36 37 38 39 |
# File 'lib/http/2/next/emitter.rb', line 35 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.
23 24 25 26 27 28 |
# File 'lib/http/2/next/emitter.rb', line 23 def once(event, &block) add_listener(event) do |*args, &callback| block.call(*args, &callback) :delete end end |