-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
callbacks.rb
74 lines (67 loc) · 2.3 KB
/
callbacks.rb
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true
require "active_support/callbacks"
module ActionCable
module Channel
# = Action Cable \Channel \Callbacks
#
# Action Cable Channel provides callback hooks that are invoked during the
# life cycle of a channel:
#
# * {before_subscribe}[rdoc-ref:ClassMethods#before_subscribe]
# * {after_subscribe}[rdoc-ref:ClassMethods#after_subscribe] (aliased as
# {on_subscribe}[rdoc-ref:ClassMethods#on_subscribe])
# * {before_unsubscribe}[rdoc-ref:ClassMethods#before_unsubscribe]
# * {after_unsubscribe}[rdoc-ref:ClassMethods#after_unsubscribe] (aliased as
# {on_unsubscribe}[rdoc-ref:ClassMethods#on_unsubscribe])
#
# ==== Example
#
# class ChatChannel < ApplicationCable::Channel
# after_subscribe :send_welcome_message, unless: :subscription_rejected?
# after_subscribe :track_subscription
#
# private
# def send_welcome_message
# broadcast_to(...)
# end
#
# def track_subscription
# # ...
# end
# end
#
module Callbacks
extend ActiveSupport::Concern
include ActiveSupport::Callbacks
included do
define_callbacks :subscribe
define_callbacks :unsubscribe
end
module ClassMethods
def before_subscribe(*methods, &block)
set_callback(:subscribe, :before, *methods, &block)
end
# This callback will be triggered after the Base#subscribed method is
# called, even if the subscription was rejected with the Base#reject
# method.
#
# To trigger the callback only on successful subscriptions, use the
# Base#subscription_rejected? method:
#
# after_subscribe :my_method, unless: :subscription_rejected?
#
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
def after_unsubscribe(*methods, &block)
set_callback(:unsubscribe, :after, *methods, &block)
end
alias_method :on_unsubscribe, :after_unsubscribe
end
end
end
end