From 0f761c0d51b8ccfd0d33562194cc5ef92199dc18 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Wed, 24 Jun 2015 18:22:16 -0400 Subject: Update API to camel cased equivalent of WebSocket's API --- lib/assets/javascripts/cable.js.coffee | 47 ++++++++++++++++++-------------- lib/assets/javascripts/channel.js.coffee | 19 ++++++------- 2 files changed, 35 insertions(+), 31 deletions(-) (limited to 'lib/assets') 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) -> -- cgit v1.2.3