module AnyCable::WithConnectionState

def cstate

def cstate
  env.cstate
end

def cstate=(val)

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

def initialize(session: nil, **other)

def initialize(session: nil, **other)
  if session
    other[:cstate] ||= {}
    other[:cstate][SESSION_KEY] = session
  end
  super(**other)
end

def istate

def istate
  env.istate
end

def istate=(val)

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

def session

def session
  state_ = cstate
  if state_
    state_[SESSION_KEY]
  end
end

def session=(val)

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