1
- import React , { PropTypes } from 'react'
1
+ import { PropTypes } from 'react'
2
2
3
3
// Works around issues with context updates failing to propagate.
4
4
// https://github.com/facebook/react/issues/2517
@@ -13,61 +13,61 @@ function makeContextName(name) {
13
13
return `@@contextSubscriber/${ name } `
14
14
}
15
15
16
- export function createContextProvider ( name ) {
16
+ export function ContextProvider ( name ) {
17
17
const contextName = makeContextName ( name )
18
+ const listenersKey = `${ contextName } /listeners`
19
+ const eventIndexKey = `${ contextName } /eventIndex`
20
+ const subscribeKey = `${ contextName } /subscribe`
18
21
19
- const ContextProvider = React . createClass ( {
20
- propTypes : {
21
- children : PropTypes . node . isRequired
22
- } ,
23
-
22
+ return {
24
23
childContextTypes : {
25
24
[ contextName ] : contextProviderShape . isRequired
26
25
} ,
27
26
28
27
getChildContext ( ) {
29
28
return {
30
29
[ contextName ] : {
31
- subscribe : this . subscribe ,
32
- eventIndex : this . eventIndex
30
+ eventIndex : this [ eventIndexKey ] ,
31
+ subscribe : this [ subscribeKey ]
33
32
}
34
33
}
35
34
} ,
36
35
37
36
componentWillMount ( ) {
38
- this . eventIndex = 0
39
- this . listeners = [ ]
37
+ this [ listenersKey ] = [ ]
38
+ this [ eventIndexKey ] = 0
40
39
} ,
41
40
42
41
componentWillReceiveProps ( ) {
43
- this . eventIndex ++
42
+ this [ eventIndexKey ] ++
44
43
} ,
45
44
46
45
componentDidUpdate ( ) {
47
- this . listeners . forEach ( listener => listener ( this . eventIndex ) )
46
+ this [ listenersKey ] . forEach ( listener =>
47
+ listener ( this [ eventIndexKey ] )
48
+ )
48
49
} ,
49
50
50
- subscribe ( listener ) {
51
+ [ subscribeKey ] ( listener ) {
51
52
// No need to immediately call listener here.
52
- this . listeners . push ( listener )
53
+ this [ listenersKey ] . push ( listener )
53
54
54
55
return ( ) => {
55
- this . listeners = this . listeners . filter ( item => item !== listener )
56
+ this [ listenersKey ] = this [ listenersKey ] . filter ( item =>
57
+ item !== listener
58
+ )
56
59
}
57
- } ,
58
-
59
- render ( ) {
60
- return this . props . children
61
60
}
62
- } )
63
-
64
- return ContextProvider
61
+ }
65
62
}
66
63
67
- export function connectToContext ( WrappedComponent , name ) {
64
+ export function ContextSubscriber ( name ) {
68
65
const contextName = makeContextName ( name )
66
+ const lastRenderedEventIndexKey = `${ contextName } /lastRenderedEventIndex`
67
+ const handleContextUpdateKey = `${ contextName } /handleContextUpdate`
68
+ const unsubscribeKey = `${ contextName } /unsubscribe`
69
69
70
- const ContextSubscriber = React . createClass ( {
70
+ return {
71
71
contextTypes : {
72
72
[ contextName ] : contextProviderShape
73
73
} ,
@@ -78,7 +78,7 @@ export function connectToContext(WrappedComponent, name) {
78
78
}
79
79
80
80
return {
81
- lastRenderedEventIndex : this . context [ contextName ] . eventIndex
81
+ [ lastRenderedEventIndexKey ] : this . context [ contextName ] . eventIndex
82
82
}
83
83
} ,
84
84
@@ -87,8 +87,8 @@ export function connectToContext(WrappedComponent, name) {
87
87
return
88
88
}
89
89
90
- this . unsubscribe = this . context [ contextName ] . subscribe (
91
- this . handleContextUpdate
90
+ this [ unsubscribeKey ] = this . context [ contextName ] . subscribe (
91
+ this [ handleContextUpdateKey ]
92
92
)
93
93
} ,
94
94
@@ -98,29 +98,23 @@ export function connectToContext(WrappedComponent, name) {
98
98
}
99
99
100
100
this . setState ( {
101
- lastRenderedEventIndex : this . context [ contextName ] . eventIndex
101
+ [ lastRenderedEventIndexKey ] : this . context [ contextName ] . eventIndex
102
102
} )
103
103
} ,
104
104
105
105
componentWillUnmount ( ) {
106
- if ( ! this . unsubscribe ) {
106
+ if ( ! this [ unsubscribeKey ] ) {
107
107
return
108
108
}
109
109
110
- this . unsubscribe ( )
111
- this . unsubscribe = null
110
+ this [ unsubscribeKey ] ( )
111
+ this [ unsubscribeKey ] = null
112
112
} ,
113
113
114
- handleContextUpdate ( eventIndex ) {
115
- if ( eventIndex !== this . state . lastRenderedEventIndex ) {
116
- this . setState ( { lastRenderedEventIndex : eventIndex } )
114
+ [ handleContextUpdateKey ] ( eventIndex ) {
115
+ if ( eventIndex !== this . state [ lastRenderedEventIndexKey ] ) {
116
+ this . setState ( { [ lastRenderedEventIndexKey ] : eventIndex } )
117
117
}
118
- } ,
119
-
120
- render ( ) {
121
- return < WrappedComponent { ...this . props } />
122
118
}
123
- } )
124
-
125
- return ContextSubscriber
119
+ }
126
120
}
0 commit comments