aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/channel/callbacks.rb
blob: 15bfb9a3da9e199a5af69626d1b6a92fca4abcc6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module ActionCable
  module Channel
    module Callbacks
      extend ActiveSupport::Concern

      included do
        class_attribute :on_subscribe_callbacks, :on_unsubscribe_callbacks, :periodic_timers, :instance_reader => false

        self.on_subscribe_callbacks = []
        self.on_unsubscribe_callbacks = []
        self.periodic_timers = []
      end

      module ClassMethods
        def on_subscribe(*methods)
          self.on_subscribe_callbacks += methods
        end

        def on_unsubscribe(*methods)
          self.on_unsubscribe_callbacks += methods
        end

        def periodically(callback, every:)
          self.periodic_timers += [ [ callback, every: every ] ]
        end
      end
    end
  end
end