Class: EventedQueue
- Inherits:
-
AbstractEventedQueue
- Object
- AbstractEventedQueue
- EventedQueue
- Defined in:
- lib/evented-queue.rb
Overview
Non thread-safe queue working using callbacks usable especially for evented environments such as EventMachine
Direct Known Subclasses
Defined Under Namespace
Classes: Recurring
Instance Attribute Summary collapse
-
#callbacks ⇒ Array
Holds callbacks waiting for value.
-
#stack ⇒ UnifiedQueues::Single
Holds values stack.
Instance Method Summary collapse
-
#clear {|result| ... } ⇒ Object
(also: #clear!)
Clears the queue.
-
#empty? {|Boolean| ... } ⇒ Boolean
Indicates queue is empty.
-
#initialize(stack = UnifiedQueues::Single::new(Array)) ⇒ EventedQueue
constructor
Constructor.
-
#length {|Integer| ... } ⇒ Integer
Indicates length of the queue.
-
#pop {|Object| ... } ⇒ Object
(also: #pop!)
Pushes value out of the queue.
-
#push(value, key = value) {|nil| ... } ⇒ Object
Pushes value into the queue.
Constructor Details
#initialize(stack = UnifiedQueues::Single::new(Array)) ⇒ EventedQueue
Constructor.
35 36 37 38 |
# File 'lib/evented-queue.rb', line 35 def initialize(stack = UnifiedQueues::Single::new(Array)) @stack = stack @callbacks = [ ] end |
Instance Attribute Details
#callbacks ⇒ Array
Holds callbacks waiting for value.
27 28 29 |
# File 'lib/evented-queue.rb', line 27 def callbacks @callbacks end |
#stack ⇒ UnifiedQueues::Single
Holds values stack.
19 20 21 |
# File 'lib/evented-queue.rb', line 19 def stack @stack end |
Instance Method Details
#clear {|result| ... } ⇒ Object Also known as: clear!
Clears the queue.
108 109 110 111 112 |
# File 'lib/evented-queue.rb', line 108 def clear(&block) result = @stack.clear yield result if not block.nil? return result end |
#empty? {|Boolean| ... } ⇒ Boolean
Indicates queue is empty.
98 99 100 101 102 |
# File 'lib/evented-queue.rb', line 98 def empty?(&block) empty = @stack.empty? yield empty if not block.nil? return empty end |
#length {|Integer| ... } ⇒ Integer
Indicates length of the queue.
85 86 87 88 89 |
# File 'lib/evented-queue.rb', line 85 def length(&block) length = @stack.length yield length if not block.nil? return length end |
#pop {|Object| ... } ⇒ Object Also known as: pop!
Pushes value out of the queue.
66 67 68 69 70 71 72 73 74 |
# File 'lib/evented-queue.rb', line 66 def pop(&block) if self.empty? @callbacks << block else result = @stack.pop yield result if not block.nil? return result end end |
#push(value, key = value) {|nil| ... } ⇒ Object
Pushes value into the queue. Priority isn’t supported by default but can be obtained using the stack
argument of constructor by giving priority queue instance.
50 51 52 53 54 55 56 57 |
# File 'lib/evented-queue.rb', line 50 def push(value, key = value, &block) @stack.push(value, key) if not @callbacks.empty? self.pop &@callbacks.shift end yield if not block.nil? end |