Class: ActionCable::Connection::Base

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: {}, identifiers_json: '{}') ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
# File 'lib/anycable/actioncable/connection.rb', line 9

def initialize(env: {}, identifiers_json: '{}')
  @ids = ActiveSupport::JSON.decode(identifiers_json)
  @cached_ids = {}
  @env = env
  @coder = ActiveSupport::JSON
  @closed = false
  @transmissions = []
  @subscriptions = ActionCable::Connection::Subscriptions.new(self)
end

Instance Attribute Details

#transmissionsObject (readonly)

Returns the value of attribute transmissions.



7
8
9
# File 'lib/anycable/actioncable/connection.rb', line 7

def transmissions
  @transmissions
end

Instance Method Details

#closeObject



31
32
33
# File 'lib/anycable/actioncable/connection.rb', line 31

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/anycable/actioncable/connection.rb', line 35

def closed?
  @closed
end

#disposeObject



43
44
45
46
# File 'lib/anycable/actioncable/connection.rb', line 43

def dispose
  @closed = false
  transmissions.clear
end

#fetch_identifier(name) ⇒ Object

Fetch identifier and deserialize if neccessary



59
60
61
62
63
64
65
# File 'lib/anycable/actioncable/connection.rb', line 59

def fetch_identifier(name)
  @cached_ids[name] ||= @cached_ids.fetch(name) do
    val = @ids[name.to_s]
    next val unless val.is_a?(String)
    GlobalID::Locator.locate(val) || val
  end
end

#handle_closeObject



26
27
28
29
# File 'lib/anycable/actioncable/connection.rb', line 26

def handle_close
  # subscriptions.unsubscribe_from_all
  disconnect if respond_to?(:disconnect)
end

#handle_openObject



19
20
21
22
23
24
# File 'lib/anycable/actioncable/connection.rb', line 19

def handle_open
  connect if respond_to?(:connect)
  send_welcome_message
rescue ActionCable::Connection::Authorization::UnauthorizedError
  close
end

#identifiers_hashObject

Generate identifiers info. Converts GlobalID compatible vars to corresponding global IDs params.



50
51
52
53
54
55
56
# File 'lib/anycable/actioncable/connection.rb', line 50

def identifiers_hash
  identifiers.each_with_object({}) do |id, acc|
    obj = instance_variable_get("@#{id}")
    next unless obj
    acc[id] = obj.try(:to_gid_param) || obj
  end
end

#loggerObject



67
68
69
# File 'lib/anycable/actioncable/connection.rb', line 67

def logger
  ::Rails.logger
end

#transmit(cable_message) ⇒ Object



39
40
41
# File 'lib/anycable/actioncable/connection.rb', line 39

def transmit(cable_message)
  transmissions << encode(cable_message)
end