aboutsummaryrefslogblamecommitdiffstats
path: root/lib/assets/javascripts/cable/channel.js.coffee
blob: 645a44e14015ab5b15f15f8ecb68373fec39da72 (plain) (tree)
1
2
3
4
5
6
7
8
9
                   


                                              
                                             
 
                                                                          
                                    
                        



                                                         
 
                 
                                          





                                  
class Cable.Channel
  constructor: (@cable, params = {}, mixin) ->
    @identifier = JSON.stringify(params)
    extend(this, mixin)
    @cable.subscribers.add(@identifier, this)

  # Perform a channel action with the optional data passed as an attribute
  sendAction: (action, data = {}) ->
    data.action = action
    @sendMessage(data)

  sendMessage: (data) ->
    @cable.sendMessage(@identifier, JSON.stringify(data))

  unsubscribe: ->
    @cable.subscribers.remove(@identifier)

  extend = (object, properties) ->
    if properties?
      for key, value of properties
        object[key] = value
    object