Module: Puppeteer::ConcurrentRubyUtils

Defined in:
lib/puppeteer/concurrent_ruby_utils.rb

Overview

utility methods for Concurrent::Promises.

Instance Method Summary collapse

Instance Method Details

#await(future_or_value) ⇒ Object

blocking get value of Future.



34
35
36
37
38
39
40
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 34

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

#await_all(*args) ⇒ Object

wait for all promises. REMARK: This method doesn’t assure the order of calling. for example, await_all(async1, async2) calls calls2 -> calls1 often.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 6

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

#await_any(*args) ⇒ Object

wait for first promises. REMARK: This method doesn’t assure the order of calling. for example, await_all(async1, async2) calls calls2 -> calls1 often.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 21

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

#future(*args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 42

def future(*args, &block)
  Concurrent::Promises.future(*args) do |*block_args|
    block.call(*block_args)
  rescue Puppeteer::TimeoutError
    # suppress error logging
    raise
  rescue => err
    Logger.new($stderr).warn(err)
    raise err
  end
end

#resolvable_future(&block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 54

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