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.



75
76
77
78
79
80
81
# File 'lib/async/queue.rb', line 75

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

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



83
84
85
# File 'lib/async/queue.rb', line 83

def limit
  @limit
end

Instance Method Details

#dequeueObject



98
99
100
101
102
103
104
# File 'lib/async/queue.rb', line 98

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

#enqueue(item) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/async/queue.rb', line 90

def enqueue item
	while 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.



86
87
88
# File 'lib/async/queue.rb', line 86

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