Sha256: 9f8cfdc91804ddaa3913acf8e39d43cbd6d5877a8f696cccefb9a886ba982c7e

Contents?: true

Size: 999 Bytes

Versions: 2

Compression:

Stored size: 999 Bytes

Contents

module EventMachine
  module Synchrony
    class Multi
      include EventMachine::Deferrable

      attr_reader :requests, :responses

      def initialize
        @requests = {}
        @responses = {:callback => {}, :errback => {}}
      end

      def add(name, conn)
        raise 'Duplicate Multi key' if @requests.key? name

        @requests[name] = conn

        fiber = Fiber.current
        conn.callback { @responses[:callback][name] = conn; check_progress(fiber) }
        conn.errback  { @responses[:errback][name]  = conn; check_progress(fiber) }
      end

      def finished?
        (@responses[:callback].size + @responses[:errback].size) == @requests.size
      end

      def perform
        Fiber.yield unless finished?
      end

      protected

        def check_progress(fiber)
          if finished?
            succeed

            # continue processing
            fiber.resume(self) if fiber.alive? && fiber != Fiber.current
          end
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
em-synchrony-1.0.6 lib/em-synchrony/em-multi.rb
em-synchrony-1.0.5 lib/em-synchrony/em-multi.rb