@@ -4,24 +4,30 @@ import React, { PropTypes } from 'react'
4
4
// https://github.com/facebook/react/issues/2517
5
5
// https://github.com/reactjs/react-router/issues/470
6
6
7
- export function createContextProvider ( name , contextType = PropTypes . any ) {
7
+ const contextProviderShape = PropTypes . shape ( {
8
+ subscribe : PropTypes . func . isRequired ,
9
+ eventIndex : PropTypes . number . isRequired
10
+ } )
11
+
12
+ function makeContextName ( name ) {
13
+ return `@@contextSubscriber/${ name } `
14
+ }
15
+
16
+ export function createContextProvider ( name ) {
17
+ const contextName = makeContextName ( name )
18
+
8
19
const ContextProvider = React . createClass ( {
9
20
propTypes : {
10
21
children : PropTypes . node . isRequired
11
22
} ,
12
23
13
- contextTypes : {
14
- [ name ] : contextType
15
- } ,
16
-
17
24
childContextTypes : {
18
- [ name ] : contextType
25
+ [ contextName ] : contextProviderShape . isRequired
19
26
} ,
20
27
21
28
getChildContext ( ) {
22
29
return {
23
- [ name ] : {
24
- ...this . context [ name ] ,
30
+ [ contextName ] : {
25
31
subscribe : this . subscribe ,
26
32
eventIndex : this . eventIndex
27
33
}
@@ -58,41 +64,41 @@ export function createContextProvider(name, contextType = PropTypes.any) {
58
64
return ContextProvider
59
65
}
60
66
61
- export function connectToContext ( WrappedComponent , name , contextType = PropTypes . any ) {
67
+ export function connectToContext ( WrappedComponent , name ) {
68
+ const contextName = makeContextName ( name )
69
+
62
70
const ContextSubscriber = React . createClass ( {
63
71
contextTypes : {
64
- [ name ] : contextType
72
+ [ contextName ] : contextProviderShape
65
73
} ,
66
74
67
75
getInitialState ( ) {
68
- if ( ! this . context [ name ] ) {
76
+ if ( ! this . context [ contextName ] ) {
69
77
return { }
70
78
}
71
79
72
80
return {
73
- lastRenderedEventIndex : this . context [ name ] . eventIndex
81
+ lastRenderedEventIndex : this . context [ contextName ] . eventIndex
74
82
}
75
83
} ,
76
84
77
85
componentDidMount ( ) {
78
- if ( ! this . context [ name ] ) {
86
+ if ( ! this . context [ contextName ] ) {
79
87
return
80
88
}
81
89
82
- this . unsubscribe = this . context [ name ] . listen ( eventIndex => {
83
- if ( eventIndex !== this . state . lastRenderedEventIndex ) {
84
- this . setState ( { lastRenderedEventIndex : eventIndex } )
85
- }
86
- } )
90
+ this . unsubscribe = this . context [ contextName ] . subscribe (
91
+ this . handleContextUpdate
92
+ )
87
93
} ,
88
94
89
95
componentWillReceiveProps ( ) {
90
- if ( ! this . context [ name ] ) {
96
+ if ( ! this . context [ contextName ] ) {
91
97
return
92
98
}
93
99
94
100
this . setState ( {
95
- lastRenderedEventIndex : this . context [ name ] . eventIndex
101
+ lastRenderedEventIndex : this . context [ contextName ] . eventIndex
96
102
} )
97
103
} ,
98
104
@@ -105,6 +111,12 @@ export function connectToContext(WrappedComponent, name, contextType = PropTypes
105
111
this . unsubscribe = null
106
112
} ,
107
113
114
+ handleContextUpdate ( eventIndex ) {
115
+ if ( eventIndex !== this . state . lastRenderedEventIndex ) {
116
+ this . setState ( { lastRenderedEventIndex : eventIndex } )
117
+ }
118
+ } ,
119
+
108
120
render ( ) {
109
121
return < WrappedComponent { ...this . props } />
110
122
}
0 commit comments