Class: Async::LimitedQueue

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

Instance Attribute Summary

Attributes inherited from Queue

#items

Instance Method Summary collapse

Methods inherited from Notification

#signal

Methods inherited from Condition

#empty?, #signal, #wait

Constructor Details

#initialize(limit = 1) ⇒ LimitedQueue

Returns a new instance of LimitedQueue.



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

def initialize(limit = 1)
	super()
	
	@limit = limit
	@full = Async::Condition.new
end

Instance Method Details

#dequeueObject



70
71
72
73
74
75
76
# File 'lib/async/queue.rb', line 70

def dequeue
	item = super
	
	@full.signal unless @full.empty?
	
	return item
end

#enqueue(item) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/async/queue.rb', line 62

def enqueue item
	if limited?
		@full.wait
	end
	
	super
end

#limited?Boolean

Returns Whether trying to enqueue an item would block.

Returns:

  • (Boolean)

    Whether trying to enqueue an item would block.



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

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