|
| 1 | +import React, { PropTypes } from 'react' |
| 2 | + |
| 3 | +// Works around issues with context updates failing to propagate. |
| 4 | +// https://github.com/facebook/react/issues/2517 |
| 5 | +// https://github.com/reactjs/react-router/issues/470 |
| 6 | + |
| 7 | +export function createContextProvider(name, contextType = PropTypes.any) { |
| 8 | + const ContextProvider = React.createClass({ |
| 9 | + propTypes: { |
| 10 | + children: PropTypes.node.isRequired |
| 11 | + }, |
| 12 | + |
| 13 | + contextTypes: { |
| 14 | + [name]: contextType |
| 15 | + }, |
| 16 | + |
| 17 | + childContextTypes: { |
| 18 | + [name]: contextType |
| 19 | + }, |
| 20 | + |
| 21 | + getChildContext() { |
| 22 | + return { |
| 23 | + [name]: { |
| 24 | + ...this.context[name], |
| 25 | + subscribe: this.subscribe, |
| 26 | + eventIndex: this.eventIndex |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
| 30 | + |
| 31 | + componentWillMount() { |
| 32 | + this.eventIndex = 0 |
| 33 | + this.listeners = [] |
| 34 | + }, |
| 35 | + |
| 36 | + componentWillReceiveProps() { |
| 37 | + this.eventIndex++ |
| 38 | + }, |
| 39 | + |
| 40 | + componentDidUpdate() { |
| 41 | + this.listeners.forEach(listener => listener(this.eventIndex)) |
| 42 | + }, |
| 43 | + |
| 44 | + subscribe(listener) { |
| 45 | + // No need to immediately call listener here. |
| 46 | + this.listeners.push(listener) |
| 47 | + |
| 48 | + return () => { |
| 49 | + this.listeners = this.listeners.filter(item => item !== listener) |
| 50 | + } |
| 51 | + }, |
| 52 | + |
| 53 | + render() { |
| 54 | + return this.props.children |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + return ContextProvider |
| 59 | +} |
| 60 | + |
| 61 | +export function connectToContext(WrappedComponent, name, contextType = PropTypes.any) { |
| 62 | + const ContextSubscriber = React.createClass({ |
| 63 | + contextTypes: { |
| 64 | + [name]: contextType |
| 65 | + }, |
| 66 | + |
| 67 | + getInitialState() { |
| 68 | + if (!this.context[name]) { |
| 69 | + return {} |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + lastRenderedEventIndex: this.context[name].eventIndex |
| 74 | + } |
| 75 | + }, |
| 76 | + |
| 77 | + componentDidMount() { |
| 78 | + if (!this.context[name]) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + this.unsubscribe = this.context[name].listen(eventIndex => { |
| 83 | + if (eventIndex !== this.state.lastRenderedEventIndex) { |
| 84 | + this.setState({ lastRenderedEventIndex: eventIndex }) |
| 85 | + } |
| 86 | + }) |
| 87 | + }, |
| 88 | + |
| 89 | + componentWillReceiveProps() { |
| 90 | + if (!this.context[name]) { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + this.setState({ |
| 95 | + lastRenderedEventIndex: this.context[name].eventIndex |
| 96 | + }) |
| 97 | + }, |
| 98 | + |
| 99 | + componentWillUnmount() { |
| 100 | + if (!this.unsubscribe) { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + this.unsubscribe() |
| 105 | + this.unsubscribe = null |
| 106 | + }, |
| 107 | + |
| 108 | + render() { |
| 109 | + return <WrappedComponent {...this.props} /> |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + return ContextSubscriber |
| 114 | +} |
0 commit comments