class Aws::EventEmitter

Attributes

encoder[RW]
signal_queue[RW]
stream[RW]
validate_event[RW]

Public Class Methods

new() click to toggle source
# File lib/aws-sdk-core/event_emitter.rb, line 6
def initialize
  @listeners = {}
  @validate_event = true
  @status = :sleep
  @signal_queue = Queue.new
end

Public Instance Methods

emit(type, params) click to toggle source
# File lib/aws-sdk-core/event_emitter.rb, line 32
def emit(type, params)
  unless @stream
    raise Aws::Errors::SignalEventError.new(
      "Singaling events before making async request"\
      " is not allowed."
    )
  end
  if @validate_event && type != :end_stream
    Aws::ParamValidator.validate!(
      @encoder.rules.shape.member(type), params)
  end
  _ready_for_events?
  @stream.data(
    @encoder.encode(type, params),
    end_stream: type == :end_stream
  )
end
on(type, callback) click to toggle source
# File lib/aws-sdk-core/event_emitter.rb, line 21
def on(type, callback)
  (@listeners[type] ||= []) << callback
end
signal(type, event) click to toggle source
# File lib/aws-sdk-core/event_emitter.rb, line 25
def signal(type, event)
  return unless @listeners[type]
  @listeners[type].each do |listener|
    listener.call(event) if event.event_type == type
  end
end

Private Instance Methods

_ready_for_events?() click to toggle source
# File lib/aws-sdk-core/event_emitter.rb, line 52
def _ready_for_events?
  return true if @status == :ready

  # blocked until once initial 200 response is received
  # signal will be available in @signal_queue
  # and this check will no longer be blocked
  @signal_queue.pop
  @status = :ready
  true
end