@@ -130,84 +130,15 @@ def wait
130130 end
131131 end
132132
133- # A queue which limits the number of items that can be enqueued.
134- # @public Since *Async v1*.
133+ # @private
135134 class LimitedQueue < Queue
136- # Create a new limited queue.
137- #
138- # @parameter limit [Integer] The maximum number of items that can be enqueued.
139- # @parameter full [Notification] The notification to use for signaling when the queue is full.
140- def initialize ( limit = 1 , full : Notification . new , **options )
141- super ( **options )
142-
143- @limit = limit
144- @full = full
145- end
146-
147- # @attribute [Integer] The maximum number of items that can be enqueued.
148- attr :limit
149-
150- # Close the queue, causing all waiting tasks to return `nil`. Any subsequent calls to {enqueue} will raise an exception.
151- # Also signals all tasks waiting for the queue to be full.
152- def close
153- super
154-
155- while @full . waiting?
156- @full . signal ( nil )
157- end
158- end
159-
160- # @returns [Boolean] Whether trying to enqueue an item would block.
161- def limited?
162- !@closed && @items . size >= @limit
163- end
164-
165- # Add an item to the queue.
166- #
167- # If the queue is full, this method will block until there is space available.
168- #
169- # @parameter item [Object] The item to add to the queue.
170- def push ( item )
171- while limited?
172- @full . wait
173- end
174-
175- super
176- end
177-
178- # Add multiple items to the queue.
179- #
180- # If the queue is full, this method will block until there is space available.
181- #
182- # @parameter items [Array] The items to add to the queue.
183- def enqueue ( *items )
184- while !items . empty?
185- while limited?
186- @full . wait
187- end
188-
189- if @closed
190- raise ClosedError , "Cannot enqueue items to a closed queue."
191- end
192-
193- available = @limit - @items . size
194- @items . concat ( items . shift ( available ) )
195-
196- @available . signal unless self . empty?
197- end
198- end
199-
200- # Remove and return the next item from the queue.
201- #
202- # If the queue is empty, this method will block until an item is available.
203- #
204- # @returns [Object] The next item in the queue.
205- def dequeue
206- item = super
135+ # This gets redefined if you load `async/limited_queue`.
136+ def self . new ( ...)
137+ warn "Use `require 'async/limited_queue'` to use `Async::LimitedQueue` without a warning." , uplevel : 1
207138
208- @full . signal
139+ require_relative "limited_queue"
209140
210- return item
141+ super ( ... )
211142 end
212143 end
213144end
0 commit comments