aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/app/assets/javascripts/action_cable/source/connection.coffee
diff options
context:
space:
mode:
authorJon Moss <me@jonathanmoss.me>2016-01-30 20:39:22 -0500
committerJon Moss <me@jonathanmoss.me>2016-01-30 20:44:42 -0500
commit09a706065952d58d515420b19a55df619eb7f53d (patch)
tree422b1b5473e7784c56e9a8f5a2bf070a86b87530 /actioncable/app/assets/javascripts/action_cable/source/connection.coffee
parent896950a605c509f19f3e8cbde11e23ca87036ca3 (diff)
downloadrails-09a706065952d58d515420b19a55df619eb7f53d.tar.gz
rails-09a706065952d58d515420b19a55df619eb7f53d.tar.bz2
rails-09a706065952d58d515420b19a55df619eb7f53d.zip
Improvements and reorganization of assets
Diffstat (limited to 'actioncable/app/assets/javascripts/action_cable/source/connection.coffee')
-rw-r--r--actioncable/app/assets/javascripts/action_cable/source/connection.coffee81
1 files changed, 81 insertions, 0 deletions
diff --git a/actioncable/app/assets/javascripts/action_cable/source/connection.coffee b/actioncable/app/assets/javascripts/action_cable/source/connection.coffee
new file mode 100644
index 0000000000..fbd7dbd35b
--- /dev/null
+++ b/actioncable/app/assets/javascripts/action_cable/source/connection.coffee
@@ -0,0 +1,81 @@
+# Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation.
+
+{message_types} = ActionCable.INTERNAL
+
+class ActionCable.Connection
+ @reopenDelay: 500
+
+ constructor: (@consumer) ->
+ @open()
+
+ send: (data) ->
+ if @isOpen()
+ @webSocket.send(JSON.stringify(data))
+ true
+ else
+ false
+
+ open: =>
+ if @webSocket and not @isState("closed")
+ throw new Error("Existing connection must be closed before opening")
+ else
+ @webSocket = new WebSocket(@consumer.url)
+ @installEventHandlers()
+ true
+
+ close: ->
+ @webSocket?.close()
+
+ reopen: ->
+ if @isState("closed")
+ @open()
+ else
+ try
+ @close()
+ finally
+ setTimeout(@open, @constructor.reopenDelay)
+
+ isOpen: ->
+ @isState("open")
+
+ # Private
+
+ isState: (states...) ->
+ @getState() in states
+
+ getState: ->
+ return state.toLowerCase() for state, value of WebSocket when value is @webSocket?.readyState
+ null
+
+ installEventHandlers: ->
+ for eventName of @events
+ handler = @events[eventName].bind(this)
+ @webSocket["on#{eventName}"] = handler
+ return
+
+ events:
+ message: (event) ->
+ {identifier, message, type} = JSON.parse(event.data)
+
+ switch type
+ when message_types.confirmation
+ @consumer.subscriptions.notify(identifier, "connected")
+ when message_types.rejection
+ @consumer.subscriptions.reject(identifier)
+ else
+ @consumer.subscriptions.notify(identifier, "received", message)
+
+ open: ->
+ @disconnected = false
+ @consumer.subscriptions.reload()
+
+ close: ->
+ @disconnect()
+
+ error: ->
+ @disconnect()
+
+ disconnect: ->
+ return if @disconnected
+ @disconnected = true
+ @consumer.subscriptions.notifyAll("disconnected")