Class: Async::LimitedQueue

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

Instance Attribute Summary collapse

Attributes inherited from Queue

#items

Instance Method Summary collapse

Methods inherited from Queue

#async, #each, #empty?, #size

Methods inherited from Notification

#signal

Methods inherited from Condition

#empty?, #signal, #wait

Constructor Details

#initialize(limit = 1, **options) ⇒ LimitedQueue

Returns a new instance of LimitedQueue.



66
67
68
69
70
71
72
# File 'lib/async/queue.rb', line 66

def initialize(limit = 1, **options)
	super(**options)
	
	@limit = limit
	
	@full = Notification.new
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



74
75
76
# File 'lib/async/queue.rb', line 74

def limit
  @limit
end

Instance Method Details

#<<(item) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/async/queue.rb', line 81

def <<(item)
	while limited?
		@full.wait
	end
	
	super
end

#dequeueObject



102
103
104
105
106
107
108
# File 'lib/async/queue.rb', line 102

def dequeue
	item = super
	
	@full.signal
	
	return item
end

#enqueue(*items) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/async/queue.rb', line 89

def enqueue *items
	while !items.empty?
		while limited?
			@full.wait
		end
		
		available = @limit - @items.size
		@items.concat(items.shift(available))
		
		self.signal unless self.empty?
	end
end

#limited?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/async/queue.rb', line 77

def limited?
	@items.size >= @limit
end