Sha256: 7c78020eadf62c3369e0ce0f576c3a7706532ed524e76fe66ea478c9aacc7428

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "anycable/rpc/rpc_pb"
require "anycable/rpc/rpc_services_pb"

# Extend some PB auto-generated classes
module AnyCable
  # Current RPC proto version (used for compatibility checks)
  PROTO_VERSION = "v1"
  SESSION_KEY = "_s_"

  # Add setters/getter for cstate field
  module WithConnectionState
    def initialize(session: nil, **other)
      if session
        other[:cstate] ||= {}
        other[:cstate][SESSION_KEY] = session
      end
      super(**other)
    end

    def session=(val)
      self.cstate = {} unless cstate
      cstate[SESSION_KEY] = val
    end

    def session
      cstate[SESSION_KEY]
    end

    def cstate
      env.cstate
    end

    def cstate=(val)
      env.cstate = val
    end
  end

  # Status predicates
  module StatusPredicates
    def success?
      status == :SUCCESS
    end

    def failure?
      status == :FAILURE
    end

    def error?
      status == :ERROR
    end
  end

  class ConnectionResponse
    prepend WithConnectionState
    include StatusPredicates
  end

  class CommandMessage
    prepend WithConnectionState
  end

  class CommandResponse
    prepend WithConnectionState
    include StatusPredicates
  end

  class DisconnectRequest
    prepend WithConnectionState
  end

  class DisconnectResponse
    include StatusPredicates
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
anycable-1.0.0.preview2 lib/anycable/rpc.rb
anycable-1.0.0.preview1 lib/anycable/rpc.rb