Extract periodic timers concern

上级 53c4b416
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
......@@ -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 @@ def matches?(identifier)
def initialize(connection, channel_identifier, params = {})
@connection = connection
@channel_identifier = channel_identifier
@_active_periodic_timers = []
@params = params
perform_connection
......@@ -115,19 +112,6 @@ def run_unsubscribe_callbacks
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
......
......@@ -4,11 +4,10 @@ module Callbacks
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 @@ def on_subscribe(*methods)
def on_unsubscribe(*methods)
self.on_unsubscribe_callbacks += methods
end
def periodically(callback, every:)
self.periodic_timers += [ [ callback, every: every ] ]
end
end
end
end
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册