Module: Puppeteer::ConcurrentRubyUtils
- Defined in:
- lib/puppeteer/concurrent_ruby_utils.rb
Overview
utility methods for Concurrent::Promises.
Instance Method Summary collapse
-
#await(future_or_value) ⇒ Object
blocking get value of Future.
-
#await_all(*args) ⇒ Object
wait for all promises.
-
#await_any(*args) ⇒ Object
wait for first promises.
- #future(&block) ⇒ Object
- #resolvable_future(&block) ⇒ Object
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(&block) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 42 def future(&block) Concurrent::Promises.future do block.call rescue => err Logger.new($stderr).warn(err) raise err end end |
#resolvable_future(&block) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/puppeteer/concurrent_ruby_utils.rb', line 51 def resolvable_future(&block) future = Concurrent::Promises.resolvable_future if block block.call(future) end future end |