Sha256: 67327bae986895f5d920b7a89c8f07dfef56d9a778dd508ab33cb8d9d09c712a

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module Weary
  class Batch
    
    attr_accessor :requests, :pool, :responses
    
    def initialize(*requests)
      @requests = requests.flatten
    end
    
     # A callback that is triggered after all the Responses have been received.
    def on_complete(&block)
      @on_complete = block if block_given?
      @on_complete
    end
    
    # A callback that is triggered before the Requests are performed
    def before_send(&block)
      @before_send = block if block_given?
      @before_send
    end
    
    # Perform each Request in a separate Thread. 
    # The Threads are collected in `pool`.
    # The Responses are collected in `responses`.
    # Pass in a block to use as the on_complete callback.
    def perform(&block)
      @on_complete = block if block_given?
      @responses = []
      @pool = []
      before_send.call if before_send
      requests.each {|req| @pool << req.perform! }
      pool.each {|res| @responses << res.value }
      on_complete.call if on_complete
      responses
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
weary-0.7.2 lib/weary/batch.rb
weary-0.7.1 lib/weary/batch.rb
weary-0.7.0 lib/weary/batch.rb