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
|
2015-12-16 09:29:21 -05:00
|
|
|
# us ActionCable.Subscriptions#create, and it should be called through the consumer like so:
|
2015-07-08 08:42:04 -04:00
|
|
|
#
|
|
|
|
# @App = {}
|
2015-12-16 09:29:21 -05:00
|
|
|
# App.cable = ActionCable.createConsumer "ws://example.com/accounts/1"
|
2015-07-08 08:42:04 -04:00
|
|
|
# App.appearance = App.cable.subscriptions.create "AppearanceChannel"
|
|
|
|
#
|
2015-12-16 09:29:21 -05:00
|
|
|
# For more details on how you'd configure an actual channel subscription, see ActionCable.Subscription.
|
|
|
|
class ActionCable.Subscriptions
|
2015-07-08 05:00:24 -04:00
|
|
|
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}
|
2015-12-16 09:29:21 -05:00
|
|
|
new ActionCable.Subscription this, params, mixin
|
2015-07-08 05:06:07 -04:00
|
|
|
|
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...)
|
|
|
|
|
|
|
|
sendCommand: (subscription, command) ->
|
|
|
|
{identifier} = subscription
|
2016-02-29 20:48:15 -05:00
|
|
|
@consumer.send({command, identifier})
|
|
|
|
|