1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/assets/javascripts/cable/subscriptions.js.coffee

47 lines
1.4 KiB
CoffeeScript
Raw Normal View History

class Cable.Subscriptions
constructor: (@consumer) ->
@subscriptions = []
create: (channelName, mixin) ->
channel = channelName
params = if typeof channel is "object" then channel else {channel}
new Cable.Subscription this, params, mixin
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