Skip to content

Commit 1ecb940

Browse files
committed
WIP
1 parent 4e30cd9 commit 1ecb940

13 files changed

Lines changed: 210 additions & 96 deletions

File tree

Steepfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
D = Steep::Diagnostic
2+
3+
target :lib do
4+
signature "sig"
5+
check "lib"
6+
end

gems.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
gem "rubocop"
3535
gem "rubocop-socketry"
3636

37+
gem "steep", git: "https://github.com/soutaro/steep"
38+
3739
gem "sus-fixtures-async"
3840
gem "sus-fixtures-console"
3941
gem "sus-fixtures-time"

lib/async/barrier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def empty?
6363
# Wait for all tasks to complete by invoking {Task#wait} on each waiting task, which may raise an error. As long as the task has completed, it will be removed from the barrier.
6464
#
6565
# @yields {|task| ...} If a block is given, the unwaited task is yielded. You must invoke {Task#wait} yourself. In addition, you may `break` if you have captured enough results.
66+
# @parameter task [Task] The task which has completed.
6667
#
6768
# @asynchronous Will wait for tasks to finish executing.
68-
# @rbs () ?{(Task) -> untyped} -> void
6969
def wait
7070
while !@tasks.empty?
7171
# Wait for a task to finish (we get the task node):

lib/async/idler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Idler
1212
#
1313
# @parameter maximum_load [Numeric] The maximum load before we start shedding work.
1414
# @parameter backoff [Numeric] The initial backoff time, used for delaying work.
15-
# @parameter parent [Interface(Asyncable) | Nil] The parent task to use for async operations.
15+
# @parameter parent [_Asyncable | Nil] The parent task to use for async operations.
1616
def initialize(maximum_load = 0.8, backoff: 0.01, parent: nil)
1717
@maximum_load = maximum_load
1818
@backoff = backoff
@@ -24,7 +24,7 @@ def initialize(maximum_load = 0.8, backoff: 0.01, parent: nil)
2424
# @asynchronous Executes the given block concurrently.
2525
#
2626
# @parameter arguments [Array] The arguments to pass to the block.
27-
# @parameter parent [Interface(Asyncable) | Nil] The parent task to use for async operations.
27+
# @parameter parent [_Asyncable] The parent task to use for async operations.
2828
# @parameter options [Hash] The options to pass to the task.
2929
# @yields {|task| ...} When the system is idle, the block will be executed in a new task.
3030
def async(*arguments, parent: (@parent or Task.current), **options, &block)

lib/async/list.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def prepend(node)
8383

8484
# Add the node, yield, and the remove the node.
8585
# @yields {|node| ...} Yields the node.
86+
# @parameter node [Node] The node to add to the list.
8687
# @returns [Object] Returns the result of the block.
8788
def stack(node, &block)
8889
append(node)

lib/async/notification.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Async
1111
class Notification < Condition
1212
# Signal to a given task that it should resume operations.
1313
#
14-
# @returns [Boolean] if a task was signalled.
14+
# @returns [bool] If a task was signalled.
1515
def signal(value = nil, task: Task.current)
1616
return false if @waiting.empty?
1717

@@ -21,14 +21,19 @@ def signal(value = nil, task: Task.current)
2121
end
2222

2323
Signal = Struct.new(:waiting, :value) do
24+
# @returns [bool] Returns true if the signal is still alive.
2425
def alive?
2526
true
2627
end
2728

29+
# Transfer the value to all waiting fibers.
30+
# @returns [self]
2831
def transfer
2932
waiting.each do |fiber|
3033
fiber.transfer(value) if fiber.alive?
3134
end
35+
36+
return self
3237
end
3338
end
3439

lib/async/queue.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ClosedError < RuntimeError
2323

2424
# Create a new queue.
2525
#
26-
# @parameter parent [Interface(Asyncable) | Nil] The parent task to use for async operations.
26+
# @parameter parent [_Asyncable | Nil] The parent task to use for async operations.
2727
# @parameter available [Notification] The notification to use for signaling when items are available.
2828
def initialize(parent: nil, available: Notification.new)
2929
@items = []
@@ -104,7 +104,7 @@ def pop
104104
# @asynchronous Executes the given block concurrently for each item.
105105
#
106106
# @parameter arguments [Array] The arguments to pass to the block.
107-
# @parameter parent [Interface(Asyncable) | Nil] The parent task to use for async operations.
107+
# @parameter parent [_Asyncable | Nil] The parent task to use for async operations.
108108
# @parameter options [Hash] The options to pass to the task.
109109
# @yields {|task| ...} When the system is idle, the block will be executed in a new task.
110110
def async(parent: (@parent or Task.current), **options, &block)

lib/async/task.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def run(*arguments)
225225
# @yields {|task| ...} in the context of the new task.
226226
# @raises [FinishedError] If the task has already finished.
227227
# @returns [Task] The child task.
228-
# @rbs [ResultType] (*untyped, **untyped) { () -> ResultType } -> Task[ResultType]
228+
# @rbs [T] (*untyped, **untyped) { (Task[T], *untyped, **untyped) -> T } -> Task[T]
229229
def async(*arguments, **options, &block)
230230
raise FinishedError if self.finished?
231231

@@ -370,7 +370,7 @@ def self.current
370370
end
371371

372372
# Check if there is a task defined for the current fiber.
373-
# @returns [Interface(Asyncable) | Nil]
373+
# @returns [_Asyncable | Nil]
374374
def self.current?
375375
Fiber.current.async_task
376376
end

lib/async/variable.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module Async
99
# A synchronization primitive that allows one task to wait for another task to resolve a value.
10+
# @rbs generic T
1011
class Variable
1112
# Create a new variable.
1213
#
@@ -22,13 +23,14 @@ def initialize(condition = Condition.new)
2223
#
2324
# @parameter value [Object] The value to resolve.
2425
def resolve(value = true)
25-
@value = value
26-
condition = @condition
27-
@condition = nil
28-
29-
self.freeze
30-
31-
condition.signal(value)
26+
if condition = @condition
27+
@value = value
28+
@condition = nil
29+
30+
self.freeze
31+
32+
condition.signal(value)
33+
end
3234
end
3335

3436
# Alias for {#resolve}.
@@ -45,7 +47,7 @@ def resolved?
4547

4648
# Wait for the value to be resolved.
4749
#
48-
# @returns [Object] The resolved value.
50+
# @returns [T?] The resolved value.
4951
def wait
5052
@condition&.wait
5153
return @value

lib/async/waiter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Async
1010
class Waiter
1111
# Create a waiter instance.
1212
#
13-
# @parameter parent [Interface(Asyncable) | Nil] The parent task to use for asynchronous operations.
13+
# @parameter parent [_Asyncable | Nil] The parent task to use for asynchronous operations.
1414
# @parameter finished [::Async::Condition] The condition to signal when a task completes.
1515
def initialize(parent: nil, finished: Async::Condition.new)
1616
warn("`Async::Waiter` is deprecated, use `Async::Barrier` instead.", uplevel: 1, category: :deprecated) if $VERBOSE

0 commit comments

Comments
 (0)