Sha256: 96f7663809e7662c815ea9aa719f2c2b1aed14cf92f97127ec60f8eebaec56ba

Contents?: true

Size: 1.33 KB

Versions: 76

Compression:

Stored size: 1.33 KB

Contents

module Aws
  class EventEmitter

    def initialize
      @listeners = {}
      @validate_event = true
      @status = :sleep
      @signal_queue = Queue.new
    end

    attr_accessor :stream

    attr_accessor :encoder

    attr_accessor :validate_event

    attr_accessor :signal_queue

    def on(type, callback)
      (@listeners[type] ||= []) << callback
    end

    def signal(type, event)
      return unless @listeners[type]
      @listeners[type].each do |listener|
        listener.call(event) if event.event_type == type
      end
    end

    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

    private

    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

  end
end

Version data entries

76 entries across 76 versions & 1 rubygems

Version Path
aws-sdk-core-3.100.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.99.2 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.99.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.99.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.98.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.97.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.97.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.96.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.96.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.95.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.94.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.94.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.93.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.92.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.91.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.91.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.90.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.90.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.89.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.89.0 lib/aws-sdk-core/event_emitter.rb