Sha256: c4d428a6272195d3a9372908ddf3a988494071337e8a4382e7ef07ff88959e15

Contents?: true

Size: 1.15 KB

Versions: 25

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  module LanguageServer
    # How you'd use this class:
    #
    # In thread #1:
    #   def foo
    #     chan = Channel.create
    #     send_request(chan.id, ...)
    #     result = chan.pop
    #     do_stuff_with_result(result)
    #   ensure
    #     chan.close
    #   end
    #
    # In thread #2:
    #   Channel.by_id(id) << result
    class Channel
      MUTEX = Mutex.new
      CHANNELS = {}

      class << self
        def create
          id = new_id
          CHANNELS[id] = new(id)
          CHANNELS[id]
        end

        def by_id(id)
          CHANNELS[id]
        end

        def close(id)
          CHANNELS.delete(id)
        end

        private

        def new_id
          MUTEX.synchronize do
            @id ||= 0
            @id += 1
          end
        end
      end

      attr_reader :id

      def initialize(id)
        @id = id
        @response = SizedQueue.new(1)
      end

      def pop
        @response.pop
      end

      def <<(value)
        @response << value
      end

      def close
        @response.close
        Channel.close(id)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.2.0 lib/platformos_check/language_server/channel.rb
platformos-check-0.1.0 lib/platformos_check/language_server/channel.rb
platformos-check-0.0.3 lib/platformos_check/language_server/channel.rb
platformos-check-0.0.2 lib/platformos_check/language_server/channel.rb
platformos-check-0.0.1 lib/platformos_check/language_server/channel.rb