File tree Expand file tree Collapse file tree 2 files changed +43
-32
lines changed Expand file tree Collapse file tree 2 files changed +43
-32
lines changed Original file line number Diff line number Diff line change @@ -5,20 +5,25 @@ const createBroadcast = (initialValue) => {
5
5
let listeners = [ ]
6
6
let currentValue = initialValue
7
7
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
+
8
23
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
22
27
}
23
28
}
24
29
@@ -48,7 +53,7 @@ class Broadcast extends React.Component {
48
53
49
54
return {
50
55
...broadcasts ,
51
- [ channel ] : this . broadcast . subscribe
56
+ [ channel ] : this . broadcast
52
57
}
53
58
}
54
59
@@ -65,7 +70,7 @@ class Broadcast extends React.Component {
65
70
)
66
71
67
72
if ( this . props . value !== nextProps . value )
68
- this . broadcast . publish ( nextProps . value )
73
+ this . broadcast . setValue ( nextProps . value )
69
74
}
70
75
71
76
render ( ) {
Original file line number Diff line number Diff line change @@ -14,29 +14,35 @@ class Subscriber extends React.Component {
14
14
value : null
15
15
}
16
16
17
- componentWillMount ( ) {
18
- const { channel } = this . props
17
+ getBroadcast ( ) {
18
+ return this . context . broadcasts [ this . props . channel ]
19
+ }
19
20
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
+ }
22
35
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 ( )
29
38
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
+ } )
35
42
}
36
43
37
44
componentWillUnmount ( ) {
38
- if ( this . unsubscribe )
39
- this . unsubscribe ( )
45
+ this . unsubscribe ( )
40
46
}
41
47
42
48
render ( ) {
You can’t perform that action at this time.
0 commit comments