aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/action_cable/channel.rb3
-rw-r--r--lib/action_cable/channel/base.rb18
-rw-r--r--lib/action_cable/channel/callbacks.rb7
-rw-r--r--lib/action_cable/channel/periodic_timers.rb38
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