-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
channel.rb
59 lines (44 loc) · 1.34 KB
/
channel.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
# frozen_string_literal: true
require "action_cable"
ActionCable::Channel::Base.prepend(Module.new do
def subscribe_to_channel
super unless anycabled? && !@__anycable_subscribing__
end
def handle_subscribe
@__anycable_subscribing__ = true
subscribe_to_channel
ensure
@__anycable_subscribing__ = false
end
def start_periodic_timers
super unless anycabled?
end
def stop_periodic_timers
super unless anycabled?
end
def stream_from(broadcasting, _callback = nil, **opts)
whispering = opts.delete(:whisper)
return super unless anycabled?
broadcasting = String(broadcasting)
connection.anycable_socket.subscribe identifier, broadcasting
if whispering
connection.anycable_socket.whisper identifier, broadcasting
end
end
def stream_for(model, callback = nil, **opts, &block)
stream_from(broadcasting_for(model), callback || block, **opts)
end
def stop_stream_from(broadcasting)
return super unless anycabled?
connection.anycable_socket.unsubscribe identifier, broadcasting
end
def stop_all_streams
return super unless anycabled?
connection.anycable_socket.unsubscribe_from_all identifier
end
private
def anycabled?
# Use instance variable check here for testing compatibility
connection.instance_variable_defined?(:@anycable_socket)
end
end)