From cf426a7ee680e8cd30a4b5afccf7e140537836f4 Mon Sep 17 00:00:00 2001 From: "Mike A. Owens" Date: Mon, 24 Aug 2015 13:35:59 -0400 Subject: Use ActiveSupport::Callbacks for Channel subscription callbacks. * Rely on AS::Callbacks for callback handling. * Add before_subscribe, after_subscribe, before_unsubscribe and after_unsubscribe convenience methods * alias on_subscribe and on_unsubscribe to after_subscribe and after_unsubscribe, respectively. * Remove `subscribed` and `unsubscribed` from the callback chain: these methods are now executed as the subject of the callbacks. * Update portions of ActionCable to use the more specific callback names. --- lib/action_cable/channel/base.rb | 19 +++++---------- lib/action_cable/channel/callbacks.rb | 38 +++++++++++++++++------------ lib/action_cable/channel/periodic_timers.rb | 6 ++--- 3 files changed, 32 insertions(+), 31 deletions(-) (limited to 'lib') diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 2f1b4a187d..171558e371 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -71,9 +71,6 @@ module ActionCable include Naming include Broadcasting - on_subscribe :subscribed - on_unsubscribe :unsubscribed - attr_reader :params, :connection delegate :logger, to: :connection @@ -138,7 +135,9 @@ module ActionCable # Called by the cable connection when its cut so the channel has a chance to cleanup with callbacks. # This method is not intended to be called directly by the user. Instead, overwrite the #unsubscribed callback. def unsubscribe_from_channel - run_unsubscribe_callbacks + run_callbacks :unsubscribe do + unsubscribed + end logger.info "#{self.class.name} unsubscribed" end @@ -176,7 +175,9 @@ module ActionCable def subscribe_to_channel logger.info "#{self.class.name} subscribing" - run_subscribe_callbacks + run_callbacks :subscribe do + subscribed + end end @@ -205,14 +206,6 @@ module ActionCable end end end - - def run_subscribe_callbacks - self.class.on_subscribe_callbacks.each { |callback| send(callback) } - end - - def run_unsubscribe_callbacks - self.class.on_unsubscribe_callbacks.each { |callback| send(callback) } - end end end end diff --git a/lib/action_cable/channel/callbacks.rb b/lib/action_cable/channel/callbacks.rb index dcdd27b9a7..f050fc95c0 100644 --- a/lib/action_cable/channel/callbacks.rb +++ b/lib/action_cable/channel/callbacks.rb @@ -1,28 +1,36 @@ +require 'active_support/callbacks' + module ActionCable module Channel module Callbacks - extend ActiveSupport::Concern + extend ActiveSupport::Concern + include ActiveSupport::Callbacks included do - class_attribute :on_subscribe_callbacks, :on_unsubscribe_callbacks, instance_reader: false - - self.on_subscribe_callbacks = [] - self.on_unsubscribe_callbacks = [] + define_callbacks :subscribe + define_callbacks :unsubscribe end - module ClassMethods - # Name methods that should be called when the channel is subscribed to. - # (These methods should be private, so they're not callable by the user). - def on_subscribe(*methods) - self.on_subscribe_callbacks += methods + class_methods do + def before_subscribe(*methods, &block) + set_callback(:subscribe, :before, *methods, &block) + end + + def after_subscribe(*methods, &block) + set_callback(:subscribe, :after, *methods, &block) + end + alias_method :on_subscribe, :after_subscribe + + + def before_unsubscribe(*methods, &block) + set_callback(:unsubscribe, :before, *methods, &block) end - # Name methods that should be called when the channel is unsubscribed from. - # (These methods should be private, so they're not callable by the user). - def on_unsubscribe(*methods) - self.on_unsubscribe_callbacks += methods + def after_unsubscribe(*methods, &block) + set_callback(:unsubscribe, :after, *methods, &block) end + alias_method :on_unsubscribe, :after_unsubscribe end end end -end \ No newline at end of file +end diff --git a/lib/action_cable/channel/periodic_timers.rb b/lib/action_cable/channel/periodic_timers.rb index 9bdcc87aa5..25fe8e5e54 100644 --- a/lib/action_cable/channel/periodic_timers.rb +++ b/lib/action_cable/channel/periodic_timers.rb @@ -7,8 +7,8 @@ module ActionCable class_attribute :periodic_timers, instance_reader: false self.periodic_timers = [] - on_subscribe :start_periodic_timers - on_unsubscribe :stop_periodic_timers + after_subscribe :start_periodic_timers + after_unsubscribe :stop_periodic_timers end module ClassMethods @@ -38,4 +38,4 @@ module ActionCable end end end -end \ No newline at end of file +end -- cgit v1.2.3 From fdbf759ac56eea40765bd2d45eaf9453011b5bd3 Mon Sep 17 00:00:00 2001 From: Alex Peattie Date: Tue, 6 Oct 2015 19:59:27 +0100 Subject: Fix NoMethodError when using a custom Rails.logger class --- lib/action_cable/connection/base.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index 08a75156a3..84393845c4 100644 --- a/lib/action_cable/connection/base.rb +++ b/lib/action_cable/connection/base.rb @@ -56,7 +56,7 @@ module ActionCable def initialize(server, env) @server, @env = server, env - @logger = new_tagged_logger + @logger = new_tagged_logger || server.logger @websocket = ActionCable::Connection::WebSocket.new(env) @heartbeat = ActionCable::Connection::Heartbeat.new(self) @@ -177,8 +177,10 @@ module ActionCable # Tags are declared in the server but computed in the connection. This allows us per-connection tailored tags. def new_tagged_logger - TaggedLoggerProxy.new server.logger, - tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize } + if server.logger.respond_to?(:tagged) + TaggedLoggerProxy.new server.logger, + tags: server.config.log_tags.map { |tag| tag.respond_to?(:call) ? tag.call(request) : tag.to_s.camelize } + end end def started_request_message -- cgit v1.2.3 From 351a663e57f2f20b3cd43b9ed751430665463659 Mon Sep 17 00:00:00 2001 From: Diego Ballona Date: Sat, 31 Oct 2015 18:50:54 -0200 Subject: Fixing subscription callbacks --- lib/action_cable/channel/base.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 7607b5ad59..2d528dfdbf 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -143,7 +143,9 @@ module ActionCable # Called by the cable connection when its cut so the channel has a chance to cleanup with callbacks. # This method is not intended to be called directly by the user. Instead, overwrite the #unsubscribed callback. def unsubscribe_from_channel - _run_unsubscribe_callbacks { unsubscribed } + run_callbacks :unsubscribe do + unsubscribed + end end @@ -192,7 +194,9 @@ module ActionCable def subscribe_to_channel - _run_subscribe_callbacks { subscribed } + run_callbacks :subscribe do + subscribed + end transmit_subscription_confirmation unless defer_subscription_confirmation? end @@ -227,7 +231,6 @@ module ActionCable unless subscription_confirmation_sent? logger.info "#{self.class.name} is transmitting the subscription confirmation" connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE) - @subscription_confirmation_sent = true end end -- cgit v1.2.3 From 7c1631fa48b8862f37d1026b4f0cf1061dd6947a Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 4 Nov 2015 12:38:43 -0600 Subject: Make sure cable closes the connection if open when responding to an invalid request --- lib/action_cable/connection/base.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index a629f29643..ac45124a28 100644 --- a/lib/action_cable/connection/base.rb +++ b/lib/action_cable/connection/base.rb @@ -151,7 +151,6 @@ module ActionCable server.add_connection(self) rescue ActionCable::Connection::Authorization::UnauthorizedError respond_to_invalid_request - close end def on_message(message) @@ -186,6 +185,8 @@ module ActionCable end def respond_to_invalid_request + close if websocket.alive? + logger.info finished_request_message [ 404, { 'Content-Type' => 'text/plain' }, [ 'Page not found' ] ] end -- cgit v1.2.3