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.
26
27
28
29
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 26
def initialize(connection)
@connection = connection
@subscriptions = {}
end
|
Instance Method Details
#add(identifier, subscribe = true) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 35
def add(identifier, subscribe = true)
raise AlreadySubscribedError if find(identifier)
params = connection.coder.decode(identifier)
channel_id = params.delete("channel")
channel_class = LiteCable.channel_registry.lookup(channel_id)
raise UnknownChannelError, channel_id unless channel_class
subscriptions[identifier] = channel_class.new(connection, identifier, params)
subscribe ? subscribe_channel(subscriptions[identifier]) : subscriptions[identifier]
end
|
#execute_command(data) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 67
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
78
79
80
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 78
def find(identifier)
subscriptions[identifier]
end
|
#find!(identifier) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 82
def find!(identifier)
channel = find(identifier)
raise ChannelNotFoundError unless channel
channel
end
|
#identifiers ⇒ Object
31
32
33
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 31
def identifiers
subscriptions.keys
end
|
62
63
64
65
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 62
def perform_action(identifier, data)
channel = find!(identifier)
channel.handle_action data
end
|
#remove(identifier) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 50
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
58
59
60
|
# File 'lib/lite_cable/connection/subscriptions.rb', line 58
def remove_all
subscriptions.keys.each(&method(:remove))
end
|