aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/action_cable/connection/subscriptions.rb4
-rw-r--r--lib/assets/javascripts/cable.js.coffee5
-rw-r--r--lib/assets/javascripts/channel.js.coffee43
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