Sha256: 8db60e9bb9411ea30c9857da70ed175e585105295dd0534b096998df630c95d1

Contents?: true

Size: 1.7 KB

Versions: 65

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Faraday
  class Adapter
    class EMSynchrony < Faraday::Adapter
      # A parallel manager for EMSynchrony.
      class ParallelManager
        # Add requests to queue.
        #
        # @param request [EM::HttpRequest]
        # @param method [Symbol, String] HTTP method
        # @param args [Array] the rest of the positional arguments
        def add(request, method, *args, &block)
          queue << {
            request: request,
            method: method,
            args: args,
            block: block
          }
        end

        # Run all requests on queue with `EM::Synchrony::Multi`, wrapping
        # it in a reactor and fiber if needed.
        def run
          result = nil
          if !EM.reactor_running?
            EM.run do
              Fiber.new do
                result = perform
                EM.stop
              end.resume
            end
          else
            result = perform
          end
          result
        end

        private

        # The request queue.
        def queue
          @queue ||= []
        end

        # Main `EM::Synchrony::Multi` performer.
        def perform
          multi = ::EM::Synchrony::Multi.new

          queue.each do |item|
            method = "a#{item[:method]}".to_sym

            req = item[:request].send(method, *item[:args])
            req.callback(&item[:block])

            req_name = "req_#{multi.requests.size}".to_sym
            multi.add(req_name, req)
          end

          # Clear the queue, so parallel manager objects can be reused.
          @queue = []

          # Block fiber until all requests have returned.
          multi.perform
        end
      end
    end
  end
end

Version data entries

65 entries across 53 versions & 16 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/faraday-1.2.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
avalara_sdk-24.2.29 vendor/bundle/ruby/2.7.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.0.1/lib/faraday/adapter/em_synchrony/parallel_manager.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
mx-platform-ruby-0.4.0 vendor/bundle/ruby/2.6.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
mx-platform-ruby-0.3.2 vendor/bundle/ruby/2.6.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
mx-platform-ruby-0.3.1 vendor/bundle/ruby/2.6.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
passbase-1.3.0 vendor/bundle/ruby/2.7.0/gems/faraday-em_synchrony-1.0.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/2.7.0/gems/faraday-1.3.0/lib/faraday/adapter/em_synchrony/parallel_manager.rb