Sha256: f84ceb15a3a2614e335494b86392809fcc881defff36ccfad477889701a878d4

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

# utility methods for Concurrent::Promises.
module Puppeteer::ConcurrentRubyUtils
  # wait for all promises.
  # REMARK: This method doesn't assure the order of calling.
  # for example, await_all(async1, async2) calls calls2 -> calls1 often.
  def await_all(*args)
    if args.length == 1 && args.first.is_a?(Enumerable)
      await_all(*args.first)
    else
      if args.any? { |arg| !arg.is_a?(Concurrent::Promises::Future) }
        raise ArgumentError.new("All argument must be a Future: #{args}")
      end

      Concurrent::Promises.zip(*args).value!
    end
  end

  # wait for first promises.
  # REMARK: This method doesn't assure the order of calling.
  # for example, await_all(async1, async2) calls calls2 -> calls1 often.
  def await_any(*args)
    if args.length == 1 && args.first.is_a?(Enumerable)
      await_any(*args.first)
    else
      if args.any? { |arg| !arg.is_a?(Concurrent::Promises::Future) }
        raise ArgumentError.new("All argument must be a Future: #{args}")
      end

      Concurrent::Promises.any(*args).value!
    end
  end

  # blocking get value of Future.
  def await(future_or_value)
    if future_or_value.is_a?(Concurrent::Promises::Future)
      future_or_value.value!
    else
      future_or_value
    end
  end

  def future(&block)
    Concurrent::Promises.future do
      block.call
    rescue => err
      Logger.new($stderr).warn(err)
      raise err
    end
  end

  def resolvable_future(&block)
    future = Concurrent::Promises.resolvable_future
    if block
      block.call(future)
    end
    future
  end
end

include Puppeteer::ConcurrentRubyUtils

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
puppeteer-ruby-0.0.25 lib/puppeteer/concurrent_ruby_utils.rb
puppeteer-ruby-0.0.23 lib/puppeteer/concurrent_ruby_utils.rb
puppeteer-ruby-0.0.22 lib/puppeteer/concurrent_ruby_utils.rb