Sha256: cd675b5439c8abc56e6d42f264af93669b12972c03f0b897d5632953cab6f841

Contents?: true

Size: 999 Bytes

Versions: 4

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

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

        @requests[name] = conn
      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

4 entries across 4 versions & 1 rubygems

Version Path
em-synchrony-1.0.4 lib/em-synchrony/em-multi.rb
em-synchrony-1.0.3 lib/em-synchrony/em-multi.rb
em-synchrony-1.0.2 lib/em-synchrony/em-multi.rb
em-synchrony-1.0.1 lib/em-synchrony/em-multi.rb