lib/anycable/rails/actioncable/connection.rb in anycable-rails-0.4.1 vs lib/anycable/rails/actioncable/connection.rb in anycable-rails-0.4.2
- old
+ new
@@ -1,20 +1,20 @@
# frozen_string_literal: true
-require "action_cable"
+require "action_cable/connection"
require "anycable/rails/refinements/subscriptions"
require "anycable/rails/actioncable/channel"
module ActionCable
module Connection
class Base # :nodoc:
using Anycable::Refinements::Subscriptions
- attr_reader :transmissions
+ attr_reader :socket
class << self
- def create(**options)
- new(**options)
+ def create(socket, **options)
+ new(socket, **options)
end
def identified_by(*identifiers)
super
Array(identifiers).each do |identifier|
@@ -23,29 +23,23 @@
end
end
end
end
- def initialize(env: {}, identifiers: '{}', subscriptions: [])
+ def initialize(socket, identifiers: '{}', subscriptions: [])
@ids = ActiveSupport::JSON.decode(identifiers)
@cached_ids = {}
- @env = env
+ @env = socket.env
@coder = ActiveSupport::JSON
- @closed = false
- @transmissions = []
+ @socket = socket
@subscriptions = ActionCable::Connection::Subscriptions.new(self)
# Initialize channels if any
subscriptions.each { |id| @subscriptions.fetch(id) }
end
- # Create a channel instance from identifier for the connection
- def channel_for(identifier)
- subscriptions.fetch(identifier)
- end
-
def handle_open
connect if respond_to?(:connect)
send_welcome_message
rescue ActionCable::Connection::Authorization::UnauthorizedError
close
@@ -54,24 +48,36 @@
def handle_close
subscriptions.unsubscribe_from_all
disconnect if respond_to?(:disconnect)
end
- def close
- @closed = true
+ # rubocop:disable Metrics/MethodLength
+ def handle_channel_command(identifier, command, data)
+ channel = subscriptions.fetch(identifier)
+ case command
+ when "subscribe"
+ channel.handle_subscribe
+ !channel.subscription_rejected?
+ when "unsubscribe"
+ subscriptions.remove_subscription(channel)
+ true
+ when "message"
+ channel.perform_action ActiveSupport::JSON.decode(data)
+ true
+ else
+ false
+ end
+ rescue Exception # rubocop:disable Lint/RescueException
+ false
end
+ # rubocop:enable Metrics/MethodLength
- def closed?
- @closed
+ def close
+ socket.close
end
def transmit(cable_message)
- transmissions << encode(cable_message)
- end
-
- def dispose
- @closed = false
- transmissions.clear
+ socket.transmit encode(cable_message)
end
# Generate identifiers info.
# Converts GlobalID compatible vars to corresponding global IDs params.
def identifiers_hash