Class: AnyCable::Socket::State

Inherits:
Object
  • Object
show all
Defined in:
lib/anycable/socket.rb

Overview

Represents the per-connection store (for example, used to keep session beetween RPC calls)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from) ⇒ State

Returns a new instance of State.



16
17
18
19
# File 'lib/anycable/socket.rb', line 16

def initialize(from)
  @source = from
  @dirty_keys = nil
end

Instance Attribute Details

#dirty_keysObject (readonly)

Returns the value of attribute dirty_keys.



14
15
16
# File 'lib/anycable/socket.rb', line 14

def dirty_keys
  @dirty_keys
end

#sourceObject (readonly)

Returns the value of attribute source.



14
15
16
# File 'lib/anycable/socket.rb', line 14

def source
  @source
end

Instance Method Details

#changed_fieldsObject



40
41
42
43
44
45
46
47
# File 'lib/anycable/socket.rb', line 40

def changed_fields
  return unless source

  keys = dirty_keys
  return if keys.nil?

  source.slice(*keys)
end

#read(key) ⇒ Object Also known as: []



21
22
23
# File 'lib/anycable/socket.rb', line 21

def read(key)
  source&.[](key)
end

#write(key, val) ⇒ Object Also known as: []=



27
28
29
30
31
32
33
34
35
36
# File 'lib/anycable/socket.rb', line 27

def write(key, val)
  return if source&.[](key) == val

  @source ||= {}

  keys = (@dirty_keys ||= [])
  keys << key

  source[key] = val
end