提交 211fd1b5 编写于 作者: J Jeremy Daer

Merge pull request #24036 from javan/actioncable-lazy-connect

Action Cable: Establish WebSocket connection when first subscription is created
......@@ -8,9 +8,6 @@ class ActionCable.Connection
constructor: (@consumer) ->
send: (data) ->
unless @isOpen()
@open()
if @isOpen()
@webSocket.send(JSON.stringify(data))
true
......@@ -18,7 +15,7 @@ class ActionCable.Connection
false
open: =>
if @isAlive()
if @isActive()
ActionCable.log("Attemped to open WebSocket, but existing socket is #{@getState()}")
throw new Error("Existing connection must be closed before opening")
else
......@@ -33,7 +30,7 @@ class ActionCable.Connection
reopen: ->
ActionCable.log("Reopening WebSocket, current state is #{@getState()}")
if @isAlive()
if @isActive()
try
@close()
catch error
......@@ -47,10 +44,10 @@ class ActionCable.Connection
isOpen: ->
@isState("open")
# Private
isActive: ->
@isState("open", "connecting")
isAlive: ->
@webSocket? and not @isState("closing", "closed")
# Private
isState: (states...) ->
@getState() in states
......
......@@ -23,3 +23,7 @@ class ActionCable.Consumer
send: (data) ->
@connection.send(data)
ensureActiveConnection: ->
unless @connection.isActive()
@connection.open()
......@@ -19,6 +19,7 @@ class ActionCable.Subscriptions
add: (subscription) ->
@subscriptions.push(subscription)
@consumer.ensureActiveConnection()
@notify(subscription, "initialized")
@sendCommand(subscription, "subscribe")
......@@ -59,4 +60,3 @@ class ActionCable.Subscriptions
sendCommand: (subscription, command) ->
{identifier} = subscription
@consumer.send({command, identifier})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册