Sha256: 493874521549394b1bc86e053068fcff288fc32c75f50501f639e15c208f4fb4

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require 'monitor'

module Tap
  class App
    
    # Queue allows thread-safe enqueing and dequeing of nodes and inputs for
    # execution.
    class Queue < Monitor
      
      attr_reader :queue
      
      # Creates a new Queue
      def initialize
        super
        @queue = []
      end
      
      # Clears self and returns an array of the enqueued methods and inputs,
      # organized by round.
      def clear
        synchronize do
          current = queue
          @queue = []
          current
        end
      end
      
      # Returns the number of enqueued methods
      def size
        synchronize do
          queue.size
        end
      end
      
      # True if no methods are enqueued
      def empty?
        synchronize { size == 0 }
      end
      
      # Enqueues the method and inputs.
      def enq(method, inputs)
        synchronize do
          queue.push [method, inputs]
        end
      end
      
      # Enqueues the method and inputs, but to the top of the queue.
      def unshift(method, inputs)
        synchronize do
          queue.unshift [method, inputs]
        end
      end
      
      # Concats an array of [method, input] entries to self.
      def concat(entries)
        synchronize do
          entries.each do |method, inputs|
            enq(method, inputs)
          end
        end
      end
      
      # Dequeues the next method and inputs as an array like
      # [method, inputs]. Returns nil if the queue is empty.
      def deq
        synchronize { queue.shift }
      end
      
      # Converts self to an array.
      def to_a
        synchronize do
          queue.dup
        end
      end
    end 
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tap-0.18.0 lib/tap/app/queue.rb
tap-0.17.1 lib/tap/app/queue.rb
tap-0.17.0 lib/tap/app/queue.rb