Sha256: 48b27939e901ed20a7b79eaa3d0e635e2f8fd9aa06ede20a847c994ed71e3f74

Contents?: true

Size: 1.36 KB

Versions: 159

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

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

159 entries across 159 versions & 1 rubygems

Version Path
aws-sdk-core-3.192.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.192.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.191.6 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.191.5 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.191.4 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.191.3 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.191.2 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.191.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.191.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.190.3 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.190.2 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.190.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.190.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.189.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.188.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.187.1 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.187.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.186.0 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.185.2 lib/aws-sdk-core/event_emitter.rb
aws-sdk-core-3.185.1 lib/aws-sdk-core/event_emitter.rb