Class: EventedQueue

Inherits:
AbstractEventedQueue show all
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

Recurring

Defined Under Namespace

Classes: Recurring

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack = UnifiedQueues::Single::new(Array)) ⇒ EventedQueue

Constructor.

Parameters:

  • stak (UnifiedQueues::Single)

    holder instance wrapped to unified queue



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

#callbacksArray

Holds callbacks waiting for value.

Returns:

  • (Array)


27
28
29
# File 'lib/evented-queue.rb', line 27

def callbacks
  @callbacks
end

#stackUnifiedQueues::Single

Holds values stack.

Returns:

  • (UnifiedQueues::Single)


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.

Yields:

  • (result)


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.

Yields:

  • (Boolean)

    true it it is, false otherwise

Returns:

  • (Boolean)

    true it it is, false otherwise



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.

Yields:

  • (Integer)

    length of the queue

Returns:

  • (Integer)

    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.

Yields:

  • (Object)

    value from the queue

Returns:

  • (Object)

    value from 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.

Parameters:

  • value (Object)

    value to push

  • key (Object) (defaults to: value)

    key fo priority purposes

Yields:

  • (nil)


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