Sha256: fbdab4b78cd1620c389e55bab0ad58cb7b0b561bbc25e9d3ca697e7390f76103

Contents?: true

Size: 807 Bytes

Versions: 3

Compression:

Stored size: 807 Bytes

Contents

require 'tamashii/manager/channel'

module Tamashii
  module Manager
    class ChannelPool < Hash
      def initialize(size = 10)
        @idle = []
        @ptr = 1

        size.times { create! }
      end

      def create!
        @idle << Channel.new(@ptr)
        @ptr += 1
      end

      def idle(channel_id = nil)
        return @idle if channel_id.nil?
        return unless self[channel_id]&.empty?
        @idle << self[channel_id]
        self[channel_id] = nil
      end

      def ready(channel)
        return if channel.empty?
        self[channel.id] = channel
        if @idle.include?(channel)
          @idle.delete(channel)
        end
        channel
      end

      def available?
        !@idle.empty?
      end

      def get_idle
        @idle.first
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tamashii-manager-0.1.7 lib/tamashii/manager/channel_pool.rb
tamashii-manager-0.1.5 lib/tamashii/manager/channel_pool.rb
tamashii-manager-0.1.4 lib/tamashii/manager/channel_pool.rb