@@ -11,35 +11,44 @@ module Async
1111 # A queue which allows items to be processed in order.
1212 # @public Since `stable-v1`.
1313 class Queue < Notification
14+ # Create a new queue.
15+ #
16+ # @parameter parent [Interface(:async) | Nil] The parent task to use for async operations.
1417 def initialize ( parent : nil )
1518 super ( )
1619
1720 @items = [ ]
1821 @parent = parent
1922 end
2023
24+ # @attribute [Array] The items in the queue.
2125 attr :items
2226
27+ # @returns [Integer] The number of items in the queue.
2328 def size
2429 @items . size
2530 end
26-
31+
32+ # @returns [Boolean] Whether the queue is empty.
2733 def empty?
2834 @items . empty?
2935 end
3036
37+ # Add an item to the queue.
3138 def <<( item )
3239 @items << item
3340
3441 self . signal unless self . empty?
3542 end
3643
44+ # Add multiple items to the queue.
3745 def enqueue ( *items )
3846 @items . concat ( items )
3947
4048 self . signal unless self . empty?
4149 end
4250
51+ # Remove and return the next item from the queue.
4352 def dequeue
4453 while @items . empty?
4554 self . wait
@@ -48,21 +57,34 @@ def dequeue
4857 @items . shift
4958 end
5059
60+ # Process each item in the queue.
61+ #
62+ # @asynchronous Executes the given block concurrently for each item.
63+ #
64+ # @parameter arguments [Array] The arguments to pass to the block.
65+ # @parameter parent [Interface(:async) | Nil] The parent task to use for async operations.
66+ # @parameter options [Hash] The options to pass to the task.
67+ # @yields {|task| ...} When the system is idle, the block will be executed in a new task.
5168 def async ( parent : ( @parent or Task . current ) , **options , &block )
5269 while item = self . dequeue
5370 parent . async ( item , **options , &block )
5471 end
5572 end
5673
74+ # Enumerate each item in the queue.
5775 def each
5876 while item = self . dequeue
5977 yield item
6078 end
6179 end
6280 end
6381
82+ # A queue which limits the number of items that can be enqueued.
6483 # @public Since `stable-v1`.
6584 class LimitedQueue < Queue
85+ # Create a new limited queue.
86+ #
87+ # @parameter limit [Integer] The maximum number of items that can be enqueued.
6688 def initialize ( limit = 1 , **options )
6789 super ( **options )
6890
@@ -71,13 +93,19 @@ def initialize(limit = 1, **options)
7193 @full = Notification . new
7294 end
7395
96+ # @attribute [Integer] The maximum number of items that can be enqueued.
7497 attr :limit
7598
7699 # @returns [Boolean] Whether trying to enqueue an item would block.
77100 def limited?
78101 @items . size >= @limit
79102 end
80103
104+ # Add an item to the queue.
105+ #
106+ # If the queue is full, this method will block until there is space available.
107+ #
108+ # @parameter item [Object] The item to add to the queue.
81109 def <<( item )
82110 while limited?
83111 @full . wait
@@ -86,7 +114,12 @@ def <<(item)
86114 super
87115 end
88116
89- def enqueue *items
117+ # Add multiple items to the queue.
118+ #
119+ # If the queue is full, this method will block until there is space available.
120+ #
121+ # @parameter items [Array] The items to add to the queue.
122+ def enqueue ( *items )
90123 while !items . empty?
91124 while limited?
92125 @full . wait
@@ -99,6 +132,11 @@ def enqueue *items
99132 end
100133 end
101134
135+ # Remove and return the next item from the queue.
136+ #
137+ # If the queue is empty, this method will block until an item is available.
138+ #
139+ # @returns [Object] The next item in the queue.
102140 def dequeue
103141 item = super
104142
0 commit comments