From 268ee5208ce513eb0b74e2354259e7991d1633c9 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Wed, 24 Jun 2015 14:26:26 -0400 Subject: Create JavaScript channels identified by their Ruby class name --- lib/action_cable/connection/subscriptions.rb | 4 +-- lib/assets/javascripts/cable.js.coffee | 5 ++++ lib/assets/javascripts/channel.js.coffee | 43 ++++++++++++---------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/lib/action_cable/connection/subscriptions.rb b/lib/action_cable/connection/subscriptions.rb index e0a3a133c5..992def173e 100644 --- a/lib/action_cable/connection/subscriptions.rb +++ b/lib/action_cable/connection/subscriptions.rb @@ -24,7 +24,7 @@ module ActionCable id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access subscription_klass = connection.server.registered_channels.detect do |channel_klass| - channel_klass.find_name == id_options[:channel] + channel_klass == id_options[:channel].safe_constantize end if subscription_klass @@ -67,4 +67,4 @@ module ActionCable end end end -end \ No newline at end of file +end diff --git a/lib/assets/javascripts/cable.js.coffee b/lib/assets/javascripts/cable.js.coffee index 7c033d3b08..5d5c3a0a53 100644 --- a/lib/assets/javascripts/cable.js.coffee +++ b/lib/assets/javascripts/cable.js.coffee @@ -23,6 +23,11 @@ class @Cable connection.onerror = @reconnect connection + createChannel: (channelName, mixin) -> + channel = channelName + params = if typeof channel is "object" then channel else {channel} + new Cable.Channel this, params, mixin + isConnected: => @connection?.readyState is 1 diff --git a/lib/assets/javascripts/channel.js.coffee b/lib/assets/javascripts/channel.js.coffee index c972334140..8bca24bd0e 100644 --- a/lib/assets/javascripts/channel.js.coffee +++ b/lib/assets/javascripts/channel.js.coffee @@ -1,33 +1,26 @@ class @Cable.Channel - constructor: (params = {}) -> - {channelName} = @constructor + constructor: (@cable, params = {}, mixin) -> + @identifier = JSON.stringify(params) + extend(this, mixin) - if channelName? - params['channel'] = channelName - @channelIdentifier = JSON.stringify params - else - throw new Error "This channel's constructor is missing the required 'channelName' property" - - cable.subscribe(@channelIdentifier, { - onConnect: @connected - onDisconnect: @disconnected - onReceiveData: @received - }) - - - connected: => - # Override in the subclass - - disconnected: => - # Override in the subclass - - received: (data) => - # Override in the subclass + @cable.subscribe @identifier, + onConnect: => @connected?() + onDisconnect: => @disconnected?() + onReceiveData: (data) => @receive?(data) # Perform a channel action with the optional data passed as an attribute perform: (action, data = {}) -> data.action = action - cable.sendData @channelIdentifier, JSON.stringify data + @cable.sendData(@identifier, JSON.stringify(data)) send: (data) -> - cable.sendData @channelIdentifier, JSON.stringify data + @cable.sendData(@identifier, JSON.stringify(data)) + + close: -> + @cable.unsubscribe(@identifier) + + extend = (object, properties) -> + if properties? + for key, value of properties + object[key] = value + object -- cgit v1.2.3