mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Create JavaScript channels identified by their Ruby class name
This commit is contained in:
parent
2ecc9b5138
commit
268ee5208c
3 changed files with 25 additions and 27 deletions
|
@ -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
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue