2015-07-08 05:00:24 -04:00
|
|
|
class Cable.Subscriptions
|
|
|
|
constructor: (@consumer) ->
|
|
|
|
@subscriptions = []
|
|
|
|
|
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 05:00:24 -04:00
|
|
|
add: (subscription) ->
|
|
|
|
@subscriptions.push(subscription)
|
|
|
|
@notify(subscription, "initialized")
|
|
|
|
if @sendCommand(subscription, "subscribe")
|
|
|
|
@notify(subscription, "connected")
|
|
|
|
|
|
|
|
reload: ->
|
|
|
|
for subscription in @subscriptions
|
|
|
|
if @sendCommand(subscription, "subscribe")
|
|
|
|
@notify(subscription, "connected")
|
|
|
|
|
|
|
|
remove: (subscription) ->
|
|
|
|
@sendCommand(subscription, "unsubscribe")
|
|
|
|
@subscriptions = (s for s in @subscriptions when s isnt subscription)
|
|
|
|
|
|
|
|
notifyAll: (callbackName, args...) ->
|
|
|
|
for subscription in @subscriptions
|
|
|
|
@notify(subscription, callbackName, args...)
|
|
|
|
|
|
|
|
notify: (subscription, callbackName, args...) ->
|
|
|
|
if typeof subscription is "string"
|
|
|
|
subscriptions = (s for s in @subscriptions when s.identifier is subscription)
|
|
|
|
else
|
|
|
|
subscriptions = [subscription]
|
|
|
|
|
|
|
|
for subscription in subscriptions
|
|
|
|
subscription[callbackName]?(args...)
|
|
|
|
|
|
|
|
sendCommand: (subscription, command) ->
|
|
|
|
{identifier} = subscription
|
|
|
|
if identifier is Cable.PING_IDENTIFIER
|
|
|
|
@consumer.connection.isOpen()
|
|
|
|
else
|
|
|
|
@consumer.send({command, identifier})
|
|
|
|
|
|
|
|
toJSON: ->
|
|
|
|
subscription.identifier for subscription in @subscriptions
|