Class: Async::Waiter
- Inherits:
-
Object
- Object
- Async::Waiter
- Defined in:
- lib/async/waiter.rb
Overview
Instance Method Summary collapse
-
#async(parent: (@parent or Task.current), **options, &block) ⇒ Object
Execute a child task and add it to the waiter.
-
#first(count = nil) ⇒ Object
Wait for the first ‘count` tasks to complete.
-
#initialize(parent: nil, finished: Async::Condition.new) ⇒ Waiter
constructor
Create a waiter instance.
-
#wait(count = nil) ⇒ Object
Wait for the first ‘count` tasks to complete.
Constructor Details
Instance Method Details
#async(parent: (@parent or Task.current), **options, &block) ⇒ Object
Execute a child task and add it to the waiter.
23 24 25 26 27 28 29 30 |
# File 'lib/async/waiter.rb', line 23 def async(parent: (@parent or Task.current), **, &block) parent.async(**) do |task| yield(task) ensure @done << task @finished.signal end end |
#first(count = nil) ⇒ Object
Wait for the first ‘count` tasks to complete.
36 37 38 39 40 41 42 43 44 |
# File 'lib/async/waiter.rb', line 36 def first(count = nil) minimum = count || 1 while @done.size < minimum @finished.wait end return @done.shift(*count) end |
#wait(count = nil) ⇒ Object
Wait for the first ‘count` tasks to complete.
48 49 50 51 52 53 54 |
# File 'lib/async/waiter.rb', line 48 def wait(count = nil) if count first(count).map(&:wait) else first.wait end end |