Class: Async::Queue
- Inherits:
-
Object
- Object
- Async::Queue
- Defined in:
- lib/async/queue.rb
Overview
A queue which allows items to be processed in order.
It has a compatible interface with Notification and Condition, except that it’s multi-value.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
- #The items in the queue.(items) ⇒ Object readonly
Instance Method Summary collapse
-
#async(parent: (@parent or Task.current), **options, &block) ⇒ Object
Process each item in the queue.
-
#dequeue ⇒ Object
(also: #pop)
Remove and return the next item from the queue.
-
#each ⇒ Object
Enumerate each item in the queue.
- #empty? ⇒ Boolean
-
#enqueue(*items) ⇒ Object
Add multiple items to the queue.
-
#initialize(parent: nil, available: Notification.new) ⇒ Queue
constructor
Create a new queue.
-
#push(item) ⇒ Object
(also: #<<)
Add an item to the queue.
-
#signal(value = nil) ⇒ Object
Signal the queue with a value, the same as #enqueue.
- #size ⇒ Object
-
#wait ⇒ Object
Wait for an item to be available, the same as #dequeue.
Constructor Details
#initialize(parent: nil, available: Notification.new) ⇒ Queue
Create a new queue.
21 22 23 24 25 |
# File 'lib/async/queue.rb', line 21 def initialize(parent: nil, available: Notification.new) @items = [] @parent = parent @available = available end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
28 29 30 |
# File 'lib/async/queue.rb', line 28 def items @items end |
#The items in the queue.(items) ⇒ Object (readonly)
28 |
# File 'lib/async/queue.rb', line 28 attr :items |
Instance Method Details
#async(parent: (@parent or Task.current), **options, &block) ⇒ Object
Process each item in the queue.
77 78 79 80 81 |
# File 'lib/async/queue.rb', line 77 def async(parent: (@parent or Task.current), **, &block) while item = self.dequeue parent.async(item, **, &block) end end |
#dequeue ⇒ Object Also known as: pop
Remove and return the next item from the queue.
58 59 60 61 62 63 64 |
# File 'lib/async/queue.rb', line 58 def dequeue while @items.empty? @available.wait end @items.shift end |
#each ⇒ Object
Enumerate each item in the queue.
84 85 86 87 88 |
# File 'lib/async/queue.rb', line 84 def each while item = self.dequeue yield item end end |
#empty? ⇒ Boolean
36 37 38 |
# File 'lib/async/queue.rb', line 36 def empty? @items.empty? end |
#enqueue(*items) ⇒ Object
Add multiple items to the queue.
51 52 53 54 55 |
# File 'lib/async/queue.rb', line 51 def enqueue(*items) @items.concat(items) @available.signal unless self.empty? end |
#push(item) ⇒ Object Also known as: <<
Add an item to the queue.
41 42 43 44 45 |
# File 'lib/async/queue.rb', line 41 def push(item) @items << item @available.signal unless self.empty? end |
#signal(value = nil) ⇒ Object
Signal the queue with a value, the same as #enqueue.
91 92 93 |
# File 'lib/async/queue.rb', line 91 def signal(value = nil) self.enqueue(value) end |
#size ⇒ Object
31 32 33 |
# File 'lib/async/queue.rb', line 31 def size @items.size end |
#wait ⇒ Object
Wait for an item to be available, the same as #dequeue.
96 97 98 |
# File 'lib/async/queue.rb', line 96 def wait self.dequeue end |