Class: Async::Queue

Inherits:
Notification show all
Defined in:
lib/async/queue.rb

Overview

A queue which allows items to be processed in order.

Direct Known Subclasses

LimitedQueue

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Notification

#signal

Methods inherited from Condition

#empty?, #signal, #wait

Constructor Details

#initialize(parent: nil) ⇒ Queue

Returns a new instance of Queue.



28
29
30
31
32
33
# File 'lib/async/queue.rb', line 28

def initialize(parent: nil)
	super()
	
	@items = []
	@parent = parent
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



35
36
37
# File 'lib/async/queue.rb', line 35

def items
  @items
end

Instance Method Details

#async(parent: (@parent or Task.current), &block) ⇒ Object



53
54
55
56
57
# File 'lib/async/queue.rb', line 53

def async(parent: (@parent or Task.current), &block)
	while item = self.dequeue
		parent.async(item, &block)
	end
end

#dequeueObject



45
46
47
48
49
50
51
# File 'lib/async/queue.rb', line 45

def dequeue
	while @items.empty?
		self.wait
	end
	
	@items.shift
end

#eachObject



59
60
61
62
63
# File 'lib/async/queue.rb', line 59

def each
	while item = self.dequeue
		yield item
	end
end

#enqueue(item) ⇒ Object Also known as: <<



37
38
39
40
41
# File 'lib/async/queue.rb', line 37

def enqueue item
	@items.push(item)
	
	self.signal unless self.empty?
end