Class: Async::Queue
- Inherits:
-
Notification
- Object
- Condition
- Notification
- Async::Queue
- Defined in:
- lib/async/queue.rb
Overview
A queue which allows items to be processed in order.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #async(parent: (@parent or Task.current), &block) ⇒ Object
- #dequeue ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #enqueue(item) ⇒ Object
-
#initialize(parent: nil) ⇒ Queue
constructor
A new instance of Queue.
- #size ⇒ Object
Methods inherited from Notification
Methods inherited from Condition
Constructor Details
#initialize(parent: nil) ⇒ Queue
Returns a new instance of Queue.
29 30 31 32 33 34 |
# File 'lib/async/queue.rb', line 29 def initialize(parent: nil) super() @items = [] @parent = parent end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
36 37 38 |
# File 'lib/async/queue.rb', line 36 def items @items end |
Instance Method Details
#<<(item) ⇒ Object
52 53 54 |
# File 'lib/async/queue.rb', line 52 def <<(item) enqueue(item) end |
#async(parent: (@parent or Task.current), &block) ⇒ Object
64 65 66 67 68 |
# File 'lib/async/queue.rb', line 64 def async(parent: (@parent or Task.current), &block) while item = self.dequeue parent.async(item, &block) end end |
#dequeue ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/async/queue.rb', line 56 def dequeue while @items.empty? self.wait end @items.shift end |
#each ⇒ Object
70 71 72 73 74 |
# File 'lib/async/queue.rb', line 70 def each while item = self.dequeue yield item end end |
#empty? ⇒ Boolean
42 43 44 |
# File 'lib/async/queue.rb', line 42 def empty? @items.empty? end |
#enqueue(item) ⇒ Object
46 47 48 49 50 |
# File 'lib/async/queue.rb', line 46 def enqueue(item) @items.push(item) self.signal unless self.empty? end |
#size ⇒ Object
38 39 40 |
# File 'lib/async/queue.rb', line 38 def size @items.size end |