class LiteCable::Connection::Subscriptions

Manage the connection channels and route messages

Attributes

connection[R]
subscriptions[R]

Public Class Methods

new(connection) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 17
def initialize(connection)
  @connection = connection
  @subscriptions = {}
end

Public Instance Methods

add(identifier, subscribe = true) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 26
def add(identifier, subscribe = true)
  raise AlreadySubscribedError if find(identifier)

  params = connection.coder.decode(identifier)

  channel_id = params.delete("channel")

  channel_class = Channel::Registry.find!(channel_id)

  subscriptions[identifier] = channel_class.new(connection, identifier, params)
  subscribe ? subscribe_channel(subscriptions[identifier]) : subscriptions[identifier]
end
execute_command(data) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 56
def execute_command(data)
  command = data.delete("command")
  case command
  when "subscribe" then add(data["identifier"])
  when "unsubscribe" then remove(data["identifier"])
  when "message" then perform_action(data["identifier"], data["data"])
  else
    raise UnknownCommandError, "Command not found #{command}"
  end
end
find(identifier) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 67
def find(identifier)
  subscriptions[identifier]
end
find!(identifier) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 71
def find!(identifier)
  channel = find(identifier)
  raise ChannelNotFoundError unless channel

  channel
end
identifiers() click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 22
def identifiers
  subscriptions.keys
end
perform_action(identifier, data) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 51
def perform_action(identifier, data)
  channel = find!(identifier)
  channel.handle_action data
end
remove(identifier) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 39
def remove(identifier)
  channel = find!(identifier)
  subscriptions.delete(identifier)
  channel.handle_unsubscribe
  log(:debug) { log_fmt("Unsubscribed from channel #{channel.class.id}") }
  transmit_subscription_cancel(channel.identifier)
end
remove_all() click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 47
def remove_all
  subscriptions.keys.each(&method(:remove))
end

Private Instance Methods

log_fmt(msg) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 108
def log_fmt(msg)
  "[connection:#{connection.identifier}] #{msg}"
end
subscribe_channel(channel) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 82
def subscribe_channel(channel)
  channel.handle_subscribe
  log(:debug) { log_fmt("Subscribed to channel #{channel.class.id}") }
  transmit_subscription_confirmation(channel.identifier)
  channel
rescue Channel::RejectedError
  subscriptions.delete(channel.identifier)
  transmit_subscription_rejection(channel.identifier)
  nil
end
transmit_subscription_cancel(identifier) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 103
def transmit_subscription_cancel(identifier)
  connection.transmit identifier: identifier,
    type: LiteCable::INTERNAL[:message_types][:cancel]
end
transmit_subscription_confirmation(identifier) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 93
def transmit_subscription_confirmation(identifier)
  connection.transmit identifier: identifier,
    type: LiteCable::INTERNAL[:message_types][:confirmation]
end
transmit_subscription_rejection(identifier) click to toggle source
# File lib/lite_cable/connection/subscriptions.rb, line 98
def transmit_subscription_rejection(identifier)
  connection.transmit identifier: identifier,
    type: LiteCable::INTERNAL[:message_types][:rejection]
end