diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2015-07-06 19:32:02 +0200 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2015-07-07 19:03:57 +0200 |
commit | e04c95e5e6af2765503b90364ef147cc0eb40cd4 (patch) | |
tree | 75581676a540f071af9a937ae748df61ae4548ad /lib | |
parent | 53c4b4160a5298ca19ce6bc9c37acc0770a5d053 (diff) | |
download | rails-e04c95e5e6af2765503b90364ef147cc0eb40cd4.tar.gz rails-e04c95e5e6af2765503b90364ef147cc0eb40cd4.tar.bz2 rails-e04c95e5e6af2765503b90364ef147cc0eb40cd4.zip |
Extract periodic timers concern
Diffstat (limited to 'lib')
-rw-r--r-- | lib/action_cable/channel.rb | 3 | ||||
-rw-r--r-- | lib/action_cable/channel/base.rb | 18 | ||||
-rw-r--r-- | lib/action_cable/channel/callbacks.rb | 7 | ||||
-rw-r--r-- | lib/action_cable/channel/periodic_timers.rb | 38 |
4 files changed, 42 insertions, 24 deletions
diff --git a/lib/action_cable/channel.rb b/lib/action_cable/channel.rb index 0432052514..9e4d3d3f93 100644 --- a/lib/action_cable/channel.rb +++ b/lib/action_cable/channel.rb @@ -1,7 +1,8 @@ module ActionCable module Channel + autoload :Base, 'action_cable/channel/base' autoload :Callbacks, 'action_cable/channel/callbacks' + autoload :PeriodicTimers, 'action_cable/channel/periodic_timers' autoload :Streams, 'action_cable/channel/streams' - autoload :Base, 'action_cable/channel/base' end end diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index fc229c9f27..ee22db4e09 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -2,14 +2,12 @@ module ActionCable module Channel class Base include Callbacks + include PeriodicTimers include Streams on_subscribe :connect on_unsubscribe :disconnect - on_subscribe :start_periodic_timers - on_unsubscribe :stop_periodic_timers - attr_reader :params, :connection delegate :logger, to: :connection @@ -22,7 +20,6 @@ module ActionCable def initialize(connection, channel_identifier, params = {}) @connection = connection @channel_identifier = channel_identifier - @_active_periodic_timers = [] @params = params perform_connection @@ -115,19 +112,6 @@ module ActionCable end - def start_periodic_timers - self.class.periodic_timers.each do |callback, options| - @_active_periodic_timers << EventMachine::PeriodicTimer.new(options[:every]) do - worker_pool.async.run_periodic_timer(self, callback) - end - end - end - - def stop_periodic_timers - @_active_periodic_timers.each { |timer| timer.cancel } - end - - def worker_pool connection.worker_pool end diff --git a/lib/action_cable/channel/callbacks.rb b/lib/action_cable/channel/callbacks.rb index 15bfb9a3da..3e61d8eb30 100644 --- a/lib/action_cable/channel/callbacks.rb +++ b/lib/action_cable/channel/callbacks.rb @@ -4,11 +4,10 @@ module ActionCable extend ActiveSupport::Concern included do - class_attribute :on_subscribe_callbacks, :on_unsubscribe_callbacks, :periodic_timers, :instance_reader => false + class_attribute :on_subscribe_callbacks, :on_unsubscribe_callbacks, :instance_reader => false self.on_subscribe_callbacks = [] self.on_unsubscribe_callbacks = [] - self.periodic_timers = [] end module ClassMethods @@ -19,10 +18,6 @@ module ActionCable def on_unsubscribe(*methods) self.on_unsubscribe_callbacks += methods end - - def periodically(callback, every:) - self.periodic_timers += [ [ callback, every: every ] ] - end end end end diff --git a/lib/action_cable/channel/periodic_timers.rb b/lib/action_cable/channel/periodic_timers.rb new file mode 100644 index 0000000000..d7f6b52e3d --- /dev/null +++ b/lib/action_cable/channel/periodic_timers.rb @@ -0,0 +1,38 @@ +module ActionCable + module Channel + module PeriodicTimers + extend ActiveSupport::Concern + + included do + class_attribute :periodic_timers, instance_reader: false + self.periodic_timers = [] + + on_subscribe :start_periodic_timers + on_unsubscribe :stop_periodic_timers + end + + module ClassMethods + def periodically(callback, every:) + self.periodic_timers += [ [ callback, every: every ] ] + end + end + + private + def active_periodic_timers + @active_periodic_timers ||= [] + end + + def start_periodic_timers + self.class.periodic_timers.each do |callback, options| + active_periodic_timers << EventMachine::PeriodicTimer.new(options[:every]) do + worker_pool.async.run_periodic_timer(self, callback) + end + end + end + + def stop_periodic_timers + active_periodic_timers.each { |timer| timer.cancel } + end + end + end +end
\ No newline at end of file |