From b5e0e58fe17b9cf5f53688ee1fac74772e565da1 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Tue, 23 Jun 2015 18:16:31 -0400 Subject: Require Cable.Channel constructors to define their channel name Function.name is not widely supported, and a function's name can be mangled by a minifier making it an unreliable property to infer the channel name from --- lib/assets/javascripts/channel.js.coffee | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'lib/assets/javascripts/channel.js.coffee') diff --git a/lib/assets/javascripts/channel.js.coffee b/lib/assets/javascripts/channel.js.coffee index 2f07affb19..c972334140 100644 --- a/lib/assets/javascripts/channel.js.coffee +++ b/lib/assets/javascripts/channel.js.coffee @@ -1,9 +1,12 @@ class @Cable.Channel constructor: (params = {}) -> - @channelName ?= "#{@underscore(@constructor.name)}_channel" + {channelName} = @constructor - params['channel'] = @channelName - @channelIdentifier = JSON.stringify params + if channelName? + params['channel'] = channelName + @channelIdentifier = JSON.stringify params + else + throw new Error "This channel's constructor is missing the required 'channelName' property" cable.subscribe(@channelIdentifier, { onConnect: @connected @@ -28,7 +31,3 @@ class @Cable.Channel send: (data) -> cable.sendData @channelIdentifier, JSON.stringify data - - - underscore: (value) -> - value.replace(/[A-Z]/g, (match) => "_#{match.toLowerCase()}").substr(1) \ No newline at end of file -- cgit v1.2.3 From 268ee5208ce513eb0b74e2354259e7991d1633c9 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Wed, 24 Jun 2015 14:26:26 -0400 Subject: Create JavaScript channels identified by their Ruby class name --- lib/assets/javascripts/channel.js.coffee | 43 +++++++++++++------------------- 1 file changed, 18 insertions(+), 25 deletions(-) (limited to 'lib/assets/javascripts/channel.js.coffee') diff --git a/lib/assets/javascripts/channel.js.coffee b/lib/assets/javascripts/channel.js.coffee index c972334140..8bca24bd0e 100644 --- a/lib/assets/javascripts/channel.js.coffee +++ b/lib/assets/javascripts/channel.js.coffee @@ -1,33 +1,26 @@ class @Cable.Channel - constructor: (params = {}) -> - {channelName} = @constructor + constructor: (@cable, params = {}, mixin) -> + @identifier = JSON.stringify(params) + extend(this, mixin) - if channelName? - params['channel'] = channelName - @channelIdentifier = JSON.stringify params - else - throw new Error "This channel's constructor is missing the required 'channelName' property" - - cable.subscribe(@channelIdentifier, { - onConnect: @connected - onDisconnect: @disconnected - onReceiveData: @received - }) - - - connected: => - # Override in the subclass - - disconnected: => - # Override in the subclass - - received: (data) => - # Override in the subclass + @cable.subscribe @identifier, + onConnect: => @connected?() + onDisconnect: => @disconnected?() + onReceiveData: (data) => @receive?(data) # Perform a channel action with the optional data passed as an attribute perform: (action, data = {}) -> data.action = action - cable.sendData @channelIdentifier, JSON.stringify data + @cable.sendData(@identifier, JSON.stringify(data)) send: (data) -> - cable.sendData @channelIdentifier, JSON.stringify data + @cable.sendData(@identifier, JSON.stringify(data)) + + close: -> + @cable.unsubscribe(@identifier) + + extend = (object, properties) -> + if properties? + for key, value of properties + object[key] = value + object -- cgit v1.2.3 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/channel.js.coffee | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'lib/assets/javascripts/channel.js.coffee') 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 From c7f00661bf0cc54a73ccdb9d27fa10b0fd806e43 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Thu, 25 Jun 2015 10:21:53 -0400 Subject: Move connection and subscriber code into their own classes --- lib/assets/javascripts/channel.js.coffee | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 lib/assets/javascripts/channel.js.coffee (limited to 'lib/assets/javascripts/channel.js.coffee') diff --git a/lib/assets/javascripts/channel.js.coffee b/lib/assets/javascripts/channel.js.coffee deleted file mode 100644 index 5196d5e03f..0000000000 --- a/lib/assets/javascripts/channel.js.coffee +++ /dev/null @@ -1,25 +0,0 @@ -class @Cable.Channel - constructor: (@cable, params = {}, mixin) -> - @identifier = JSON.stringify(params) - extend(this, mixin) - @subscribe(@identifier, this) - - # Perform a channel action with the optional data passed as an attribute - sendAction: (action, data = {}) -> - data.action = action - @sendMessage(data) - - sendMessage: (data) -> - @cable.sendMessage(@identifier, JSON.stringify(data)) - - subscribe: -> - @cable.subscribe(@identifier, this) - - unsubscribe: -> - @cable.unsubscribe(@identifier) - - extend = (object, properties) -> - if properties? - for key, value of properties - object[key] = value - object -- cgit v1.2.3