Sha256: a30975bf5c801dab0acd48f67d7418446de493623850881f830f3f2ea32460a9

Contents?: true

Size: 783 Bytes

Versions: 4

Compression:

Stored size: 783 Bytes

Contents

# frozen_string_literal: true

module Tamashii
  module Manager
    # :nodoc:
    class ChannelPool < Hash
      attr_reader :idles

      def initialize(size = 10)
        @idles = []
        @ptr = 1

        size.times { create! }
      end

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

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

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

      def available?
        !@idles.empty?
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tamashii-manager-0.2.3 lib/tamashii/manager/channel_pool.rb
tamashii-manager-0.2.2 lib/tamashii/manager/channel_pool.rb
tamashii-manager-0.2.1 lib/tamashii/manager/channel_pool.rb
tamashii-manager-0.2.0 lib/tamashii/manager/channel_pool.rb