aboutsummaryrefslogtreecommitdiffstats
path: root/lib/assets/javascripts
diff options
context:
space:
mode:
authorJavan Makhmali <javan@javan.us>2015-06-24 18:22:16 -0400
committerJavan Makhmali <javan@javan.us>2015-06-24 18:22:16 -0400
commit0f761c0d51b8ccfd0d33562194cc5ef92199dc18 (patch)
tree85a6189162b9f51e34315794338925680ad9f471 /lib/assets/javascripts
parent88585965ec00bbe9fe41bbe468bfbbf6dc0f9d89 (diff)
downloadrails-0f761c0d51b8ccfd0d33562194cc5ef92199dc18.tar.gz
rails-0f761c0d51b8ccfd0d33562194cc5ef92199dc18.tar.bz2
rails-0f761c0d51b8ccfd0d33562194cc5ef92199dc18.zip
Update API to camel cased equivalent of WebSocket's API
Diffstat (limited to 'lib/assets/javascripts')
-rw-r--r--lib/assets/javascripts/cable.js.coffee47
-rw-r--r--lib/assets/javascripts/channel.js.coffee19
2 files changed, 35 insertions, 31 deletions
diff --git a/lib/assets/javascripts/cable.js.coffee b/lib/assets/javascripts/cable.js.coffee
index 5d5c3a0a53..9fc269f994 100644
--- a/lib/assets/javascripts/cable.js.coffee
+++ b/lib/assets/javascripts/cable.js.coffee
@@ -16,11 +16,10 @@ class @Cable
createConnection: ->
connection = new WebSocket(@cableUrl)
- connection.onmessage = @receiveData
- connection.onopen = @connected
- connection.onclose = @reconnect
-
- connection.onerror = @reconnect
+ connection.onmessage = @onMessage
+ connection.onopen = @onConnect
+ connection.onclose = @onClose
+ connection.onerror = @onError
connection
createChannel: (channelName, mixin) ->
@@ -31,31 +30,40 @@ class @Cable
isConnected: =>
@connection?.readyState is 1
- sendData: (identifier, data) =>
+ sendMessage: (identifier, data) =>
if @isConnected()
@connection.send JSON.stringify { command: 'message', identifier: identifier, data: data }
- receiveData: (message) =>
+ onMessage: (message) =>
data = JSON.parse message.data
if data.identifier is '_ping'
@pingReceived(data.message)
else
- @subscribers[data.identifier]?.onReceiveData(data.message)
+ @subscribers[data.identifier]?.onMessage?(data.message)
- connected: =>
+ onConnect: =>
@startWaitingForPing()
@resetConnectionAttemptsCount()
- for identifier, callbacks of @subscribers
+ for identifier, subscriber of @subscribers
@subscribeOnServer(identifier)
- callbacks['onConnect']?()
+ subscriber.onConnect?()
- reconnect: =>
- @removeExistingConnection()
+ onClose: =>
+ @reconnect()
+
+ onError: =>
+ @reconnect()
+ disconnect: ->
+ @removeExistingConnection()
@resetPingTime()
- @disconnected()
+ for identifier, subscriber of @subscribers
+ subscriber.onDisconnect?()
+
+ reconnect: ->
+ @disconnect()
setTimeout =>
@incrementConnectionAttemptsCount()
@@ -95,21 +103,18 @@ class @Cable
resetPingTime: =>
@lastPingTime = null
- disconnected: =>
- callbacks['onDisconnect']?() for identifier, callbacks of @subscribers
-
giveUp: =>
# Show an error message
- subscribe: (identifier, callbacks) =>
- @subscribers[identifier] = callbacks
+ subscribe: (identifier, subscriber) =>
+ @subscribers[identifier] = subscriber
if @isConnected()
@subscribeOnServer(identifier)
- @subscribers[identifier]['onConnect']?()
+ subscriber.onConnect?()
unsubscribe: (identifier) =>
- @unsubscribeOnServer(identifier, 'unsubscribe')
+ @unsubscribeOnServer(identifier)
delete @subscribers[identifier]
subscribeOnServer: (identifier) =>
diff --git a/lib/assets/javascripts/channel.js.coffee b/lib/assets/javascripts/channel.js.coffee
index 8bca24bd0e..5196d5e03f 100644
--- a/lib/assets/javascripts/channel.js.coffee
+++ b/lib/assets/javascripts/channel.js.coffee
@@ -2,21 +2,20 @@ class @Cable.Channel
constructor: (@cable, params = {}, mixin) ->
@identifier = JSON.stringify(params)
extend(this, mixin)
-
- @cable.subscribe @identifier,
- onConnect: => @connected?()
- onDisconnect: => @disconnected?()
- onReceiveData: (data) => @receive?(data)
+ @subscribe(@identifier, this)
# Perform a channel action with the optional data passed as an attribute
- perform: (action, data = {}) ->
+ sendAction: (action, data = {}) ->
data.action = action
- @cable.sendData(@identifier, JSON.stringify(data))
+ @sendMessage(data)
+
+ sendMessage: (data) ->
+ @cable.sendMessage(@identifier, JSON.stringify(data))
- send: (data) ->
- @cable.sendData(@identifier, JSON.stringify(data))
+ subscribe: ->
+ @cable.subscribe(@identifier, this)
- close: ->
+ unsubscribe: ->
@cable.unsubscribe(@identifier)
extend = (object, properties) ->