File tree 2 files changed +24
-2
lines changed 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ def initialize(parent: nil, finished: Async::Condition.new)
13
13
@parent = parent
14
14
end
15
15
16
+ # Execute a child task and add it to the waiter.
17
+ # @asynchronous Executes the given block concurrently.
16
18
def async ( parent : ( @parent or Task . current ) , &block )
17
19
parent . async do |task |
18
20
yield ( task )
@@ -22,16 +24,28 @@ def async(parent: (@parent or Task.current), &block)
22
24
end
23
25
end
24
26
27
+ # Wait for the first `count` tasks to complete.
28
+ # @parameter count [Integer | Nil] The number of tasks to wait for.
29
+ # @returns [Array(Async::Task)] If an integer is given, the tasks which have completed.
30
+ # @returns [Async::Task] Otherwise, the first task to complete.
25
31
def first ( count = nil )
26
- while @done . size < count
32
+ minimum = count || 1
33
+
34
+ while @done . size < minimum
27
35
@finished . wait
28
36
end
29
37
30
38
return @done . shift ( *count )
31
39
end
32
40
41
+ # Wait for the first `count` tasks to complete.
42
+ # @parameter count [Integer | Nil] The number of tasks to wait for.
33
43
def wait ( count = nil )
34
- first ( count ) . map ( &:wait )
44
+ if count
45
+ first ( count ) . map ( &:wait )
46
+ else
47
+ first . wait
48
+ end
35
49
end
36
50
end
37
51
end
Original file line number Diff line number Diff line change 7
7
8
8
let ( :waiter ) { subject . new }
9
9
10
+ it "can wait for the first task to complete" do
11
+ waiter . async do
12
+ :result
13
+ end
14
+
15
+ expect ( waiter . wait ) . to be == :result
16
+ end
17
+
10
18
it "can wait for a subset of tasks" do
11
19
3 . times do
12
20
waiter . async do
You can’t perform that action at this time.
0 commit comments