@@ -2,26 +2,28 @@ import React from "react"
2
2
import PropTypes from "prop-types"
3
3
import invariant from "invariant"
4
4
5
- function createBroadcast ( initialState ) {
6
- let listeners = [ ]
7
- let currentState = initialState
5
+ function createBroadcast ( initialValue ) {
6
+ let currentValue = initialValue
7
+ let subscribers = [ ]
8
8
9
- const getState = ( ) => currentState
9
+ const getValue = ( ) => currentValue
10
10
11
- const setState = state => {
12
- currentState = state
13
- listeners . forEach ( listener => listener ( currentState ) )
11
+ const publish = state => {
12
+ currentValue = state
13
+ subscribers . forEach ( s => s ( currentValue ) )
14
14
}
15
15
16
- const subscribe = listener => {
17
- listeners . push ( listener )
16
+ const subscribe = subscriber => {
17
+ subscribers . push ( subscriber )
18
18
19
- return ( ) => ( listeners = listeners . filter ( item => item !== listener ) )
19
+ return ( ) => {
20
+ subscribers = subscribers . filter ( s => s !== subscriber )
21
+ }
20
22
}
21
23
22
24
return {
23
- getState ,
24
- setState ,
25
+ getValue ,
26
+ publish ,
25
27
subscribe
26
28
}
27
29
}
@@ -70,7 +72,7 @@ class Broadcast extends React.Component {
70
72
invariant ( this . props . channel === nextProps . channel , "You cannot change <Broadcast channel>" )
71
73
72
74
if ( ! this . props . compareValues ( this . props . value , nextProps . value ) ) {
73
- this . broadcast . setState ( nextProps . value )
75
+ this . broadcast . publish ( nextProps . value )
74
76
}
75
77
}
76
78
0 commit comments