Class: AnyCable::Socket

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

Overview

Socket mock to be used with application connection

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:) ⇒ Socket

Returns a new instance of Socket.



52
53
54
55
# File 'lib/anycable/socket.rb', line 52

def initialize(env:)
  @transmissions = []
  @request_env = env
end

Instance Attribute Details

#presenceObject (readonly)

Returns the value of attribute presence.



50
51
52
# File 'lib/anycable/socket.rb', line 50

def presence
  @presence
end

#transmissionsObject (readonly)

Returns the value of attribute transmissions.



50
51
52
# File 'lib/anycable/socket.rb', line 50

def transmissions
  @transmissions
end

Instance Method Details

#closeObject



103
104
105
106
107
# File 'lib/anycable/socket.rb', line 103

def close
  @closed = true
  @streams&.clear
  @stop_all_streams = true
end

#closed?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/anycable/socket.rb', line 109

def closed?
  @closed == true
end

#cstateObject



137
138
139
140
141
# File 'lib/anycable/socket.rb', line 137

def cstate
  return @cstate if defined?(@cstate)

  @cstate = env["anycable.cstate"] = State.new(env["anycable.raw_cstate"])
end

#envObject



125
126
127
128
129
# File 'lib/anycable/socket.rb', line 125

def env
  return @env if defined?(@env)

  @env = build_rack_env
end

#istateObject



131
132
133
134
135
# File 'lib/anycable/socket.rb', line 131

def istate
  return @istate if defined?(@istate)

  @istate = env["anycable.istate"] = State.new(env["anycable.raw_istate"])
end

#presence_join(broadcasting, id, info) ⇒ Object

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
# File 'lib/anycable/socket.rb', line 77

def presence_join(broadcasting, id, info)
  raise ArgumentError, "ID is required for presence tracking" unless id

  subscribe("", broadcasting)
  istate.write(PRESENCE_KEY, broadcasting)

  @presence = {type: "join", id: id, info: info}.compact
end

#presence_leave(id) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
# File 'lib/anycable/socket.rb', line 86

def presence_leave(id)
  raise ArgumentError, "ID is required for presence tracking" unless id

  @presence = {type: "leave", id: id}
end

#sessionObject



117
118
119
# File 'lib/anycable/socket.rb', line 117

def session
  cstate.read(SESSION_KEY)
end

#session=(val) ⇒ Object



121
122
123
# File 'lib/anycable/socket.rb', line 121

def session=(val)
  cstate.write(SESSION_KEY, val)
end

#stop_streams?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/anycable/socket.rb', line 113

def stop_streams?
  @stop_all_streams == true
end

#streamsObject



99
100
101
# File 'lib/anycable/socket.rb', line 99

def streams
  @streams ||= {start: [], stop: []}
end

#subscribe(_channel, broadcasting) ⇒ Object



61
62
63
# File 'lib/anycable/socket.rb', line 61

def subscribe(_channel, broadcasting)
  streams[:start] << broadcasting unless streams[:start].include?(broadcasting)
end

#transmit(websocket_message) ⇒ Object



57
58
59
# File 'lib/anycable/socket.rb', line 57

def transmit(websocket_message)
  transmissions << websocket_message
end

#unsubscribe(_channel, broadcasting) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/anycable/socket.rb', line 65

def unsubscribe(_channel, broadcasting)
  streams[:stop] << broadcasting unless streams[:stop].include?(broadcasting)

  if istate.read(WHISPER_KEY) == broadcasting
    istate.write(WHISPER_KEY, "")
  end
end

#unsubscribe_from_all(_channel) ⇒ Object



92
93
94
95
96
97
# File 'lib/anycable/socket.rb', line 92

def unsubscribe_from_all(_channel)
  @stop_all_streams = true
  if istate.read(WHISPER_KEY)
    istate.write(WHISPER_KEY, "")
  end
end

#whisper(_channel, broadcasting) ⇒ Object



73
74
75
# File 'lib/anycable/socket.rb', line 73

def whisper(_channel, broadcasting)
  istate.write(WHISPER_KEY, broadcasting)
end