提交 26bcf81a 编写于 作者: D David Heinemeier Hansson

Move Cable to ActionCable for client-side constant to avoid conflicts

上级 346a7528
#= require_self
#= require cable/consumer
#= require action_cable/consumer
@Cable =
@ActionCable =
INTERNAL: <%= ActionCable::INTERNAL.to_json %>
createConsumer: (url = @getConfig("url")) ->
new Cable.Consumer @createWebSocketURL(url)
new ActionCable.Consumer @createWebSocketURL(url)
getConfig: (name) ->
element = document.head.querySelector("meta[name='action-cable-#{name}']")
......
# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
{message_types} = Cable.INTERNAL
{message_types} = ActionCable.INTERNAL
class Cable.Connection
class ActionCable.Connection
@reopenDelay: 500
constructor: (@consumer) ->
......
# Responsible for ensuring the cable connection is in good health by validating the heartbeat pings sent from the server, and attempting
# revival reconnections if things go astray. Internal class, not intended for direct user manipulation.
class Cable.ConnectionMonitor
class ActionCable.ConnectionMonitor
@pollInterval:
min: 3
max: 30
@staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings)
identifier: Cable.INTERNAL.identifiers.ping
identifier: ActionCable.INTERNAL.identifiers.ping
constructor: (@consumer) ->
@consumer.subscriptions.add(this)
......
#= require cable/connection
#= require cable/connection_monitor
#= require cable/subscriptions
#= require cable/subscription
#= require action_cable/connection
#= require action_cable/connection_monitor
#= require action_cable/subscriptions
#= require action_cable/subscription
# The Cable.Consumer establishes the connection to a server-side Ruby Connection object. Once established,
# the Cable.ConnectionMonitor will ensure that its properly maintained through heartbeats and checking for stale updates.
# The ActionCable.Consumer establishes the connection to a server-side Ruby Connection object. Once established,
# the ActionCable.ConnectionMonitor will ensure that its properly maintained through heartbeats and checking for stale updates.
# The Consumer instance is also the gateway to establishing subscriptions to desired channels through the #createSubscription
# method.
#
# The following example shows how this can be setup:
#
# @App = {}
# App.cable = Cable.createConsumer "ws://example.com/accounts/1"
# App.cable = ActionCable.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.Consumer
# For more details on how you'd configure an actual channel subscription, see ActionCable.Subscription.
class ActionCable.Consumer
constructor: (@url) ->
@subscriptions = new Cable.Subscriptions this
@connection = new Cable.Connection this
@connectionMonitor = new Cable.ConnectionMonitor this
@subscriptions = new ActionCable.Subscriptions this
@connection = new ActionCable.Connection this
@connectionMonitor = new ActionCable.ConnectionMonitor this
send: (data) ->
@connection.send(data)
......
# A new subscription is created through the Cable.Subscriptions instance available on the consumer.
# A new subscription is created through the ActionCable.Subscriptions instance available on the consumer.
# It provides a number of callbacks and a method for calling remote procedure calls on the corresponding
# Channel instance on the server side.
#
......@@ -23,7 +23,7 @@
#
# This is how the server component would look:
#
# class AppearanceChannel < ApplicationCable::Channel
# class AppearanceChannel < ApplicationActionCable::Channel
# def subscribed
# current_user.appear
# end
......@@ -43,7 +43,7 @@
#
# The "AppearanceChannel" name is automatically mapped between the client-side subscription creation and the server-side Ruby class name.
# The AppearanceChannel#appear/away public methods are exposed automatically to client-side invocation through the @perform method.
class Cable.Subscription
class ActionCable.Subscription
constructor: (@subscriptions, params = {}, mixin) ->
@identifier = JSON.stringify(params)
extend(this, mixin)
......
# 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:
# us ActionCable.Subscriptions#create, and it should be called through the consumer like so:
#
# @App = {}
# App.cable = Cable.createConsumer "ws://example.com/accounts/1"
# App.cable = ActionCable.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
# For more details on how you'd configure an actual channel subscription, see ActionCable.Subscription.
class ActionCable.Subscriptions
constructor: (@consumer) ->
@subscriptions = []
@history = []
......@@ -14,7 +14,7 @@ class Cable.Subscriptions
create: (channelName, mixin) ->
channel = channelName
params = if typeof channel is "object" then channel else {channel}
new Cable.Subscription this, params, mixin
new ActionCable.Subscription this, params, mixin
# Private
......@@ -63,7 +63,7 @@ class Cable.Subscriptions
sendCommand: (subscription, command) ->
{identifier} = subscription
if identifier is Cable.INTERNAL.identifiers.ping
if identifier is ActionCable.INTERNAL.identifiers.ping
@consumer.connection.isOpen()
else
@consumer.send({command, identifier})
......
#= require cable
#= require action_cable
#= require_self
#= require ./channels
@App = {}
App.cable = Cable.createConsumer()
App.cable = ActionCable.createConsumer()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册