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 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::Queue.new
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

Instance Method Details

#dequeueObject



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

def dequeue
  item = super
  
  @full.enqueue(nil) unless @full.empty?
  
  return item
end

#enqueue(item) ⇒ Object



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

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

#limited?Boolean

Returns Whether trying to enqueue an item would block.

Returns:

  • (Boolean)

    Whether trying to enqueue an item would block.



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

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