Module: Puppeteer::ConcurrentRubyUtils

Defined in:
lib/puppeteer/concurrent_ruby_utils.rb

Overview

utility methods for Concurrent::Promises.

Defined Under Namespace

Modules: ConcurrentPromisesFutureExtension

Instance Method Summary collapse

Instance Method Details

#await(future_or_value) ⇒ Object

blocking get value of Future.



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

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.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 24

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.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 39

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 60

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.extend(ConcurrentPromisesFutureExtension)
end

#resolvable_future(&block) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 72

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