-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (44 loc) · 1.14 KB
/
index.js
File metadata and controls
54 lines (44 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var React = require('react')
var ActionCable = require('actioncable')
var ActionCableProvider = React.createClass({
getChildContext: function () {
return {
cable: this.cable
}
},
componentWillMount: function () {
if (this.props.cable) {
this.cable = this.props.cable
} else {
this.cable = ActionCable.createConsumer(this.props.url)
}
},
componentWillUnmount: function () {
if (!this.props.cable && this.cable) {
this.cable.disconnect()
}
},
componentWillReceiveProps: function (nextProps) {
// Props not changed
if (this.props.cable === nextProps.cable &&
this.props.url === nextProps.url) {
return
}
// cable is created by self, disconnect it
this.componentWillUnmount()
// create or assign cable
this.componentWillMount()
},
render: function () {
return this.props.children
}
})
ActionCableProvider.propTypes = {
cable: React.PropTypes.object,
url: React.PropTypes.string,
children: React.PropTypes.any
}
ActionCableProvider.childContextTypes = {
cable: React.PropTypes.object.isRequired
}
module.exports = ActionCableProvider