Skip to content

Commit 8d10a89

Browse files
taiontimdorr
authored andcommitted
Fixes to ContextSubscriber
- Use separate context object - Use correct subscribe method
1 parent b0bc5af commit 8d10a89

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

modules/ContextUtils.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@ import React, { PropTypes } from 'react'
44
// https://github.com/facebook/react/issues/2517
55
// https://github.com/reactjs/react-router/issues/470
66

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+
819
const ContextProvider = React.createClass({
920
propTypes: {
1021
children: PropTypes.node.isRequired
1122
},
1223

13-
contextTypes: {
14-
[name]: contextType
15-
},
16-
1724
childContextTypes: {
18-
[name]: contextType
25+
[contextName]: contextProviderShape.isRequired
1926
},
2027

2128
getChildContext() {
2229
return {
23-
[name]: {
24-
...this.context[name],
30+
[contextName]: {
2531
subscribe: this.subscribe,
2632
eventIndex: this.eventIndex
2733
}
@@ -58,41 +64,41 @@ export function createContextProvider(name, contextType = PropTypes.any) {
5864
return ContextProvider
5965
}
6066

61-
export function connectToContext(WrappedComponent, name, contextType = PropTypes.any) {
67+
export function connectToContext(WrappedComponent, name) {
68+
const contextName = makeContextName(name)
69+
6270
const ContextSubscriber = React.createClass({
6371
contextTypes: {
64-
[name]: contextType
72+
[contextName]: contextProviderShape
6573
},
6674

6775
getInitialState() {
68-
if (!this.context[name]) {
76+
if (!this.context[contextName]) {
6977
return {}
7078
}
7179

7280
return {
73-
lastRenderedEventIndex: this.context[name].eventIndex
81+
lastRenderedEventIndex: this.context[contextName].eventIndex
7482
}
7583
},
7684

7785
componentDidMount() {
78-
if (!this.context[name]) {
86+
if (!this.context[contextName]) {
7987
return
8088
}
8189

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+
)
8793
},
8894

8995
componentWillReceiveProps() {
90-
if (!this.context[name]) {
96+
if (!this.context[contextName]) {
9197
return
9298
}
9399

94100
this.setState({
95-
lastRenderedEventIndex: this.context[name].eventIndex
101+
lastRenderedEventIndex: this.context[contextName].eventIndex
96102
})
97103
},
98104

@@ -105,6 +111,12 @@ export function connectToContext(WrappedComponent, name, contextType = PropTypes
105111
this.unsubscribe = null
106112
},
107113

114+
handleContextUpdate(eventIndex) {
115+
if (eventIndex !== this.state.lastRenderedEventIndex) {
116+
this.setState({ lastRenderedEventIndex: eventIndex })
117+
}
118+
},
119+
108120
render() {
109121
return <WrappedComponent {...this.props} />
110122
}

0 commit comments

Comments
 (0)