123456789_123456789_123456789_123456789_123456789_

Class: ActionCable::Connection::Subscriptions

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: actioncable/lib/action_cable/connection/subscriptions.rb

Overview

Collection class for all the channel subscriptions established on a given connection. Responsible for routing incoming commands that arrive on the connection to the proper channel.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(connection) ⇒ Subscriptions

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 15

def initialize(connection)
  @connection = connection
  @subscriptions = {}
end

Instance Attribute Details

#connection (readonly, private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 73

attr_reader :connection, :subscriptions

#logger (readonly)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 74

delegate :logger, to: :connection

#subscriptions (readonly, private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 73

attr_reader :connection, :subscriptions

Instance Method Details

#add(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 33

def add(data)
  id_key = data["identifier"]
  id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access

  return if subscriptions.key?(id_key)

  subscription_klass = id_options[:channel].safe_constantize

  if subscription_klass && ActionCable::Channel::Base > subscription_klass
    subscription = subscription_klass.new(connection, id_key, id_options)
    subscriptions[id_key] = subscription
    subscription.subscribe_to_channel
  else
    logger.error "Subscription class not found: #{id_options[:channel].inspect}"
  end
end

#execute_command(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 20

def execute_command(data)
  case data["command"]
  when "subscribe"   then add data
  when "unsubscribe" then remove data
  when "message"     then perform_action data
  else
    logger.error "Received unrecognized command in #{data.inspect}"
  end
rescue Exception => e
  @connection.rescue_with_handler(e)
  logger.error "Could not execute command from (#{data.inspect}) [#{e.class} - #{e.message}]: #{e.backtrace.first(5).join(" | ")}"
end

#find(data) (private)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 76

def find(data)
  if subscription = subscriptions[data["identifier"]]
    subscription
  else
    raise "Unable to find subscription with identifier: #{data['identifier']}"
  end
end

#identifiers

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 64

def identifiers
  subscriptions.keys
end

#perform_action(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 60

def perform_action(data)
  find(data).perform_action ActiveSupport::JSON.decode(data["data"])
end

#remove(data)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 50

def remove(data)
  logger.info "Unsubscribing from channel: #{data['identifier']}"
  remove_subscription find(data)
end

#remove_subscription(subscription)

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 55

def remove_subscription(subscription)
  subscription.unsubscribe_from_channel
  subscriptions.delete(subscription.identifier)
end

#unsubscribe_from_all

[ GitHub ]

  
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 68

def unsubscribe_from_all
  subscriptions.each { |id, channel| remove_subscription(channel) }
end