Sha256: ca0a9ee4955dc45e16e4529b443f4f718a247e0c13dd72255910d65d3cc7cadc

Contents?: true

Size: 895 Bytes

Versions: 4

Compression:

Stored size: 895 Bytes

Contents

module PipeRpc
  class Hub::Socket
    def initialize(hub, args = {})
      @hub = hub
      @input = args.fetch(:input)
      @output = args.fetch(:output)
      @on_sent = []
      @on_received = []
    end

    def read
      raise ClosedError if @input.closed?
      incoming = Incoming.new(@input.gets) # blocks
      incoming.trigger(@on_received)
      incoming.to_h
    rescue JSON::JSONError => e
      write ErrorResponse.new(id: nil, error: { code: -32700, data: { message: e.message } })
    end

    def write(obj)
      raise ClosedError if @output.closed?
      outgoing = Outgoing.new(obj)
      outgoing.trigger(@on_sent)
      @output.puts outgoing.to_payload
    end

    def on_sent(&on_sent)
      @on_sent << on_sent
    end

    def on_received(&on_received)
      @on_received << on_received
    end

    def close
      @input.close
      @output.close
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pipe_rpc-1.1.2 lib/pipe_rpc/hub_socket.rb
pipe_rpc-1.1.1 lib/pipe_rpc/hub_socket.rb
pipe_rpc-1.1.0 lib/pipe_rpc/hub_socket.rb
pipe_rpc-1.0.0 lib/pipe_rpc/hub_socket.rb