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.
13
14
15
16
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 13
def initialize(connection)
@connection = connection
@subscriptions = {}
end
|
Instance Method Details
#add(identifier, subscribe = true) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 22
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
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 52
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
63
64
65
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 63
def find(identifier)
subscriptions[identifier]
end
|
#find!(identifier) ⇒ Object
67
68
69
70
71
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 67
def find!(identifier)
channel = find(identifier)
raise ChannelNotFoundError unless channel
channel
end
|
#identifiers ⇒ Object
18
19
20
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 18
def identifiers
subscriptions.keys
end
|
47
48
49
50
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 47
def perform_action(identifier, data)
channel = find!(identifier)
channel.handle_action data
end
|
#remove(identifier) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 35
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
43
44
45
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 43
def remove_all
subscriptions.keys.each(&method(:remove))
end
|