Class: LiteCable::Connection::Subscriptions
- Inherits:
-
Object
- Object
- LiteCable::Connection::Subscriptions
show all
- Includes:
- Logging
- Defined in:
- lib/lite_cable/connection/subscriptions.rb
Overview
Manage the connection channels and route messages
Defined Under Namespace
Classes: AlreadySubscribedError, ChannelNotFoundError, Error, UnknownCommandError
Constant Summary
Constants included
from Logging
Logging::PREFIX
Instance Method Summary
collapse
Methods included from Logging
logger
Constructor Details
Returns a new instance of Subscriptions.
14
15
16
17
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 14
def initialize(connection)
@connection = connection
@subscriptions = {}
end
|
Instance Method Details
#add(identifier, subscribe = true) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 23
def add(identifier, subscribe = true)
raise AlreadySubscribedError if find(identifier)
params = connection.coder.decode(identifier)
channel_id = params.delete("channel")
channel_class = Channel::Registry.find!(channel_id)
subscriptions[identifier] = channel_class.new(connection, identifier, params)
subscribe ? subscribe_channel(subscriptions[identifier]) : subscriptions[identifier]
end
|
#execute_command(data) ⇒ Object
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 53
def execute_command(data)
command = data.delete("command")
case command
when "subscribe" then add(data["identifier"])
when "unsubscribe" then remove(data["identifier"])
when "message" then perform_action(data["identifier"], data["data"])
else
raise UnknownCommandError, "Command not found #{command}"
end
end
|
#find(identifier) ⇒ Object
64
65
66
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 64
def find(identifier)
subscriptions[identifier]
end
|
#find!(identifier) ⇒ Object
68
69
70
71
72
73
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 68
def find!(identifier)
channel = find(identifier)
raise ChannelNotFoundError unless channel
channel
end
|
#identifiers ⇒ Object
19
20
21
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 19
def identifiers
subscriptions.keys
end
|
48
49
50
51
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 48
def perform_action(identifier, data)
channel = find!(identifier)
channel.handle_action data
end
|
#remove(identifier) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 36
def remove(identifier)
channel = find!(identifier)
subscriptions.delete(identifier)
channel.handle_unsubscribe
log(:debug) { log_fmt("Unsubscribed from channel #{channel.class.id}") }
transmit_subscription_cancel(channel.identifier)
end
|
#remove_all ⇒ Object
44
45
46
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 44
def remove_all
subscriptions.keys.each(&method(:remove))
end
|