Skip to content

Commit 6dc8ade

Browse files
committed
Subscriber must render in proper context
Also, broadcast.subscribe no longer emits on the same tick, but this is an implementation detail. Fixes #6
1 parent c623c79 commit 6dc8ade

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

modules/Broadcast.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@ const createBroadcast = (initialValue) => {
55
let listeners = []
66
let currentValue = initialValue
77

8+
const getValue = () =>
9+
currentValue
10+
11+
const setValue = (value) => {
12+
currentValue = value
13+
listeners.forEach(listener => listener(currentValue))
14+
}
15+
16+
const subscribe = (listener) => {
17+
listeners.push(listener)
18+
19+
return () =>
20+
listeners = listeners.filter(item => item !== listener)
21+
}
22+
823
return {
9-
publish(value) {
10-
currentValue = value
11-
listeners.forEach(listener => listener(currentValue))
12-
},
13-
subscribe(listener) {
14-
listeners.push(listener)
15-
16-
// Publish to this subscriber once immediately.
17-
listener(currentValue)
18-
19-
return () =>
20-
listeners = listeners.filter(item => item !== listener)
21-
}
24+
getValue,
25+
setValue,
26+
subscribe
2227
}
2328
}
2429

@@ -48,7 +53,7 @@ class Broadcast extends React.Component {
4853

4954
return {
5055
...broadcasts,
51-
[channel]: this.broadcast.subscribe
56+
[channel]: this.broadcast
5257
}
5358
}
5459

@@ -65,7 +70,7 @@ class Broadcast extends React.Component {
6570
)
6671

6772
if (this.props.value !== nextProps.value)
68-
this.broadcast.publish(nextProps.value)
73+
this.broadcast.setValue(nextProps.value)
6974
}
7075

7176
render() {

modules/Subscriber.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,35 @@ class Subscriber extends React.Component {
1414
value: null
1515
}
1616

17-
componentWillMount() {
18-
const { channel } = this.props
17+
getBroadcast() {
18+
return this.context.broadcasts[this.props.channel]
19+
}
1920

20-
if (this.context.broadcasts) {
21-
const subscribe = this.context.broadcasts[channel]
21+
componentWillMount() {
22+
const broadcast = this.getBroadcast()
23+
24+
invariant(
25+
broadcast,
26+
'<Subscriber channel="%s"> must be rendered in the context of a <Broadcast channel="%s">',
27+
this.props.channel,
28+
this.props.channel
29+
)
30+
31+
this.setState({
32+
value: broadcast.getValue()
33+
})
34+
}
2235

23-
invariant(
24-
typeof subscribe === 'function',
25-
'<Subscriber channel="%s"> must be rendered in the context of a <Broadcast channel="%s">',
26-
channel,
27-
channel
28-
)
36+
componentDidMount() {
37+
const broadcast = this.getBroadcast()
2938

30-
this.unsubscribe = subscribe(value => {
31-
// This function will be called once immediately.
32-
this.setState({ value })
33-
})
34-
}
39+
this.unsubscribe = broadcast.subscribe(value => {
40+
this.setState({ value })
41+
})
3542
}
3643

3744
componentWillUnmount() {
38-
if (this.unsubscribe)
39-
this.unsubscribe()
45+
this.unsubscribe()
4046
}
4147

4248
render() {

0 commit comments

Comments
 (0)