Skip to content

Commit

Permalink
Add setChannel method to cable; only bind channels if they exist
Browse files Browse the repository at this point in the history
  • Loading branch information
schneidmaster committed Dec 22, 2015
1 parent ed39107 commit bdfa91b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/action_cable_react.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/channel_mixin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/cable.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ class Cable
channel: (name) ->
@channels[name]

setChannel: (name, channel) ->
@channels[name] = channel

module.exports = Cable
32 changes: 17 additions & 15 deletions lib/channel_mixin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@ ChannelMixin = ->
@mounted = true

for channel in channelNames
cable.channel(channel).on 'connected', @handleConnected if @handleConnected?
cable.channel(channel).on 'disconnected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).on 'rejected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).on 'received', @handleReceived if @handleReceived?
if cable.channel(channel)
cable.channel(channel).on 'connected', @handleConnected if @handleConnected?
cable.channel(channel).on 'disconnected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).on 'rejected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).on 'received', @handleReceived if @handleReceived?

for action in cable.channel(channel).actions
actionMethod = "handle#{_capitalize(action)}"
cable.channel(channel).on action, @[actionMethod] if @[actionMethod]?
for action in cable.channel(channel).actions
actionMethod = "handle#{_capitalize(action)}"
cable.channel(channel).on action, @[actionMethod] if @[actionMethod]?

componentWillUnmount: ->
cable = @props.cable or @context.cable
@mounted = false
for channel in channelNames
cable.channel(channel).removeListener 'connected', @handleConnected if @handleConnected?
cable.channel(channel).removeListener 'disconnected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).removeListener 'rejected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).removeListener 'received', @handleReceived if @handleReceived?

for action in cable.channel(channel).actions
actionMethod = "handle#{_capitalize(action)}"
cable.channel(channel).removeListener action, @[actionMethod] if @[actionMethod]?
if cable.channel(channel)
cable.channel(channel).removeListener 'connected', @handleConnected if @handleConnected?
cable.channel(channel).removeListener 'disconnected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).removeListener 'rejected', @handleDisconnected if @handleDisconnected?
cable.channel(channel).removeListener 'received', @handleReceived if @handleReceived?

for action in cable.channel(channel).actions
actionMethod = "handle#{_capitalize(action)}"
cable.channel(channel).removeListener action, @[actionMethod] if @[actionMethod]?

perform: (channel, action, data = {}) ->
cable = @props.cable or @context.cable
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "action-cable-react",
"version": "0.1.0",
"version": "0.1.1",
"description": "Use Rails 5 ActionCable channels with React for realtime magic.",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = '0.1.0'
module.exports = '0.1.1'

0 comments on commit bdfa91b

Please sign in to comment.