/
subscriptions.rb
52 lines (44 loc) · 1.38 KB
/
subscriptions.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
module ActionCable
module Connection
class Subscriptions
def initialize(connection)
@connection = connection
@subscriptions = {}
end
def add(data)
id_key = data['identifier']
id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access
subscription_klass = connection.server.registered_channels.detect do |channel_klass|
channel_klass.find_name == id_options[:channel]
end
if subscription_klass
subscriptions[id_key] = subscription_klass.new(connection, id_key, id_options)
else
connection.logger.error "Subscription class not found (#{data.inspect})"
end
end
def remove(data)
connection.logger.info "Unsubscribing from channel: #{data['identifier']}"
subscriptions[data['identifier']].perform_disconnection
subscriptions.delete(data['identifier'])
end
def find(identifier)
if subscription = subscriptions[identifier]
subscription
else
raise "Unable to find subscription with identifier: #{identifier}"
end
end
def identifiers
subscriptions.keys
end
def cleanup
subscriptions.each do |id, channel|
channel.perform_disconnection
end
end
private
attr_reader :connection, :subscriptions
end
end
end