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

#signal, #wait

Constructor Details

#initialize(parent: nil) ⇒ Queue

Returns a new instance of Queue.



14
15
16
17
18
19
# File 'lib/async/queue.rb', line 14

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

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



21
22
23
# File 'lib/async/queue.rb', line 21

def items
  @items
end

Instance Method Details

#<<(item) ⇒ Object



31
32
33
34
35
# File 'lib/async/queue.rb', line 31

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

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



51
52
53
54
55
# File 'lib/async/queue.rb', line 51

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

#dequeueObject



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

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

#eachObject



57
58
59
60
61
# File 'lib/async/queue.rb', line 57

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

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/async/queue.rb', line 27

def empty?
	@items.empty?
end

#enqueue(*items) ⇒ Object



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

def enqueue(*items)
	@items.concat(items)
	
	self.signal unless self.empty?
end

#sizeObject



23
24
25
# File 'lib/async/queue.rb', line 23

def size
	@items.size
end