diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-09-02 02:57:38 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-09-02 02:57:38 -0300 |
commit | eb8c713c987480e7a0362ae3de617ba0c0f27d7f (patch) | |
tree | 7f5b9afd7a6326161c75270701f1e880e4f20263 /lib/assets/javascripts/cable/subscriptions.coffee | |
parent | 0cf1db6be29fb2269d722bedd690641e0f949b36 (diff) | |
download | rails-eb8c713c987480e7a0362ae3de617ba0c0f27d7f.tar.gz rails-eb8c713c987480e7a0362ae3de617ba0c0f27d7f.tar.bz2 rails-eb8c713c987480e7a0362ae3de617ba0c0f27d7f.zip |
.js.coffee -> .coffee
It was initially required, but support for the shorthand has been
supported since sprockets 2.1. Eventually 4.x will only support the
shorthand version. Just want to get new people using the prefer stuff
ASAP.
Diffstat (limited to 'lib/assets/javascripts/cable/subscriptions.coffee')
-rw-r--r-- | lib/assets/javascripts/cable/subscriptions.coffee | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee new file mode 100644 index 0000000000..eeaa697081 --- /dev/null +++ b/lib/assets/javascripts/cable/subscriptions.coffee @@ -0,0 +1,71 @@ +# 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 = {} +# App.cable = Cable.createConsumer "ws://example.com/accounts/1" +# App.appearance = App.cable.subscriptions.create "AppearanceChannel" +# +# For more details on how you'd configure an actual channel subscription, see Cable.Subscription. +class Cable.Subscriptions + constructor: (@consumer) -> + @subscriptions = [] + @history = [] + + create: (channelName, mixin) -> + channel = channelName + params = if typeof channel is "object" then channel else {channel} + new Cable.Subscription this, params, mixin + + # Private + + 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) -> + @subscriptions = (s for s in @subscriptions when s isnt subscription) + unless @findAll(subscription.identifier).length + @sendCommand(subscription, "unsubscribe") + + findAll: (identifier) -> + s for s in @subscriptions when s.identifier is identifier + + notifyAll: (callbackName, args...) -> + for subscription in @subscriptions + @notify(subscription, callbackName, args...) + + notify: (subscription, callbackName, args...) -> + if typeof subscription is "string" + subscriptions = @findAll(subscription) + else + subscriptions = [subscription] + + for subscription in subscriptions + subscription[callbackName]?(args...) + + if callbackName in ["initialized", "connected", "disconnected"] + {identifier} = subscription + @record(notification: {identifier, callbackName, args}) + + sendCommand: (subscription, command) -> + {identifier} = subscription + if identifier is Cable.PING_IDENTIFIER + @consumer.connection.isOpen() + else + @consumer.send({command, identifier}) + + record: (data) -> + data.time = new Date() + @history = @history.slice(-19) + @history.push(data) + + toJSON: -> + history: @history + identifiers: (subscription.identifier for subscription in @subscriptions) |