class Async::Queue

@public Since ‘stable-v1`.
It has a compatible interface with {Notification} and {Condition}, except that it’s multi-value.
A queue which allows items to be processed in order.

def <<(item)

Add an item to the queue.
def <<(item)
	@items << item
	
	@available.signal unless self.empty?
end

def async(parent: (@parent or Task.current), **options, &block)

@yields {|task| ...} When the system is idle, the block will be executed in a new task.
@parameter options [Hash] The options to pass to the task.
@parameter parent [Interface(:async) | Nil] The parent task to use for async operations.
@parameter arguments [Array] The arguments to pass to the block.

@asynchronous Executes the given block concurrently for each item.

Process each item in the queue.
def async(parent: (@parent or Task.current), **options, &block)
	while item = self.dequeue
		parent.async(item, **options, &block)
	end
end

def dequeue

Remove and return the next item from the queue.
def dequeue
	while @items.empty?
		@available.wait
	end
	
	@items.shift
end

def each

Enumerate each item in the queue.
def each
	while item = self.dequeue
		yield item
	end
end

def empty?

@returns [Boolean] Whether the queue is empty.
def empty?
	@items.empty?
end

def enqueue(*items)

Add multiple items to the queue.
def enqueue(*items)
	@items.concat(items)
	
	@available.signal unless self.empty?
end

def initialize(parent: nil, available: Notification.new)

@parameter available [Notification] The notification to use for signaling when items are available.
@parameter parent [Interface(:async) | Nil] The parent task to use for async operations.

Create a new queue.
def initialize(parent: nil, available: Notification.new)
	@items = []
	@parent = parent
	@available = available
end

def signal(value)

Signal the queue with a value, the same as {#enqueue}.
def signal(value)
	self.enqueue(value)
end

def size

@returns [Integer] The number of items in the queue.
def size
	@items.size
end

def wait

Wait for an item to be available, the same as {#dequeue}.
def wait
	self.dequeue
end