2015-07-08 08:42:04 -04:00
|
|
|
# Collection class for creating (and internally managing) channel subscriptions. The only method intended to be triggered by the user
|
|
|
|
# us Cable.Subscriptions#create, and it should be called through the consumer like so:
|
|
|
|
#
|
|
|
|
# @App = {}
|
2015-07-09 03:21:11 -04:00
|
|
|
# App.cable = Cable.createConsumer "ws://example.com/accounts/1"
|
2015-07-08 08:42:04 -04:00
|
|
|
# App.appearance = App.cable.subscriptions.create "AppearanceChannel"
|
|
|
|
#
|
|
|
|
# For more details on how you'd configure an actual channel subscription, see Cable.Subscription.
|
2015-07-08 05:00:24 -04:00
|
|
|
class Cable.Subscriptions
|
|
|
|
constructor: (@consumer) ->
|
|
|
|
@subscriptions = []
|
2015-08-23 17:58:06 -04:00
|
|
|
@history = []
|
2015-07-08 05:00:24 -04:00
|
|
|
|
2015-07-08 05:06:07 -04:00
|
|
|
create: (channelName, mixin) ->
|
|
|
|
channel = channelName
|
|
|
|
params = if typeof channel is "object" then channel else {channel}
|
|
|
|
new Cable.Subscription this, params, mixin
|
|
|
|
|
2015-07-08 08:42:04 -04:00
|
|
|
# Private
|
|
|
|
|
2015-07-08 05:00:24 -04:00
|
|
|
add: (subscription) ->
|
|
|
|
@subscriptions.push(subscription)
|
|
|
|
@notify(subscription, "initialized")
|
2015-10-16 22:05:33 -04:00
|
|
|
@sendCommand(subscription, "subscribe")
|
2015-07-08 05:00:24 -04:00
|
|
|
|
|
|
|
remove: (subscription) ->
|
2015-10-22 11:53:19 -04:00
|
|
|
@forget(subscription)
|
2015-10-20 18:17:18 -04:00
|
|
|
|
2015-07-23 17:20:04 -04:00
|
|
|
unless @findAll(subscription.identifier).length
|
|
|
|
@sendCommand(subscription, "unsubscribe")
|
|
|
|
|
2015-10-22 11:53:19 -04:00
|
|
|
reject: (identifier) ->
|
|
|
|
for subscription in @findAll(identifier)
|
|
|
|
@forget(subscription)
|
|
|
|
@notify(subscription, "rejected")
|
|
|
|
|
|
|
|
forget: (subscription) ->
|
2015-10-20 18:17:18 -04:00
|
|
|
@subscriptions = (s for s in @subscriptions when s isnt subscription)
|
|
|
|
|
2015-07-23 17:20:04 -04:00
|
|
|
findAll: (identifier) ->
|
|
|
|
s for s in @subscriptions when s.identifier is identifier
|
2015-07-08 05:00:24 -04:00
|
|
|
|
2015-10-22 11:53:19 -04:00
|
|
|
reload: ->
|
|
|
|
for subscription in @subscriptions
|
|
|
|
@sendCommand(subscription, "subscribe")
|
|
|
|
|
2015-07-08 05:00:24 -04:00
|
|
|
notifyAll: (callbackName, args...) ->
|
|
|
|
for subscription in @subscriptions
|
|
|
|
@notify(subscription, callbackName, args...)
|
|
|
|
|
|
|
|
notify: (subscription, callbackName, args...) ->
|
|
|
|
if typeof subscription is "string"
|
2015-07-23 17:20:04 -04:00
|
|
|
subscriptions = @findAll(subscription)
|
2015-07-08 05:00:24 -04:00
|
|
|
else
|
|
|
|
subscriptions = [subscription]
|
|
|
|
|
|
|
|
for subscription in subscriptions
|
|
|
|
subscription[callbackName]?(args...)
|
|
|
|
|
2015-10-20 18:17:18 -04:00
|
|
|
if callbackName in ["initialized", "connected", "disconnected", "rejected"]
|
2015-08-23 17:58:06 -04:00
|
|
|
{identifier} = subscription
|
|
|
|
@record(notification: {identifier, callbackName, args})
|
|
|
|
|
2015-07-08 05:00:24 -04:00
|
|
|
sendCommand: (subscription, command) ->
|
|
|
|
{identifier} = subscription
|
2015-11-06 14:09:38 -05:00
|
|
|
if identifier is Cable.INTERNAL.identifiers.ping
|
2015-07-08 05:00:24 -04:00
|
|
|
@consumer.connection.isOpen()
|
|
|
|
else
|
|
|
|
@consumer.send({command, identifier})
|
|
|
|
|
2015-08-23 17:58:06 -04:00
|
|
|
record: (data) ->
|
|
|
|
data.time = new Date()
|
|
|
|
@history = @history.slice(-19)
|
|
|
|
@history.push(data)
|
|
|
|
|
2015-07-08 05:00:24 -04:00
|
|
|
toJSON: ->
|
2015-08-23 17:58:06 -04:00
|
|
|
history: @history
|
|
|
|
identifiers: (subscription.identifier for subscription in @subscriptions)
|