diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-10-23 20:15:40 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-10-23 20:15:40 -0200 |
commit | 9f1938d9c10b4892d939374b49362e726023b5d7 (patch) | |
tree | 104afc2b0a7f6f14a6fba5cede64b4fe65b557d1 /lib/action_cable/channel | |
parent | 1db8c56cefe2e74d01ad5f3d4d4da1329013e2d6 (diff) | |
parent | cf426a7ee680e8cd30a4b5afccf7e140537836f4 (diff) | |
download | rails-9f1938d9c10b4892d939374b49362e726023b5d7.tar.gz rails-9f1938d9c10b4892d939374b49362e726023b5d7.tar.bz2 rails-9f1938d9c10b4892d939374b49362e726023b5d7.zip |
Merge pull request #69 from mieko/callbacks
Use ActiveSupport::Callbacks for Channel subscription callbacks.
Diffstat (limited to 'lib/action_cable/channel')
-rw-r--r-- | lib/action_cable/channel/base.rb | 16 | ||||
-rw-r--r-- | lib/action_cable/channel/callbacks.rb | 37 | ||||
-rw-r--r-- | lib/action_cable/channel/periodic_timers.rb | 6 |
3 files changed, 27 insertions, 32 deletions
diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 221730dbc4..7607b5ad59 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -75,9 +75,6 @@ module ActionCable SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE = 'confirm_subscription'.freeze - on_subscribe :subscribed - on_unsubscribe :unsubscribed - attr_reader :params, :connection delegate :logger, to: :connection @@ -146,7 +143,7 @@ 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_unsubscribe_callbacks { unsubscribed } end @@ -195,7 +192,7 @@ module ActionCable def subscribe_to_channel - run_subscribe_callbacks + _run_subscribe_callbacks { subscribed } transmit_subscription_confirmation unless defer_subscription_confirmation? end @@ -226,14 +223,6 @@ module ActionCable 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 - def transmit_subscription_confirmation unless subscription_confirmation_sent? logger.info "#{self.class.name} is transmitting the subscription confirmation" @@ -242,7 +231,6 @@ module ActionCable @subscription_confirmation_sent = true end end - end end end diff --git a/lib/action_cable/channel/callbacks.rb b/lib/action_cable/channel/callbacks.rb index dcdd27b9a7..295d750e86 100644 --- a/lib/action_cable/channel/callbacks.rb +++ b/lib/action_cable/channel/callbacks.rb @@ -1,28 +1,35 @@ +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 |