(VeinComponent)
| 4 | let injectionIdIndex = 0; |
| 5 | |
| 6 | const Injectable = (VeinComponent) => { |
| 7 | injectionIdIndex++; |
| 8 | const injectionId = `injectionId_${injectionIdIndex}`; |
| 9 | |
| 10 | class InjectableComponent extends Component { |
| 11 | static injectionId = injectionId; |
| 12 | |
| 13 | static contextTypes = { |
| 14 | registerInjectable: PropTypes.func.isRequired, |
| 15 | removeInjectable: PropTypes.func.isRequired |
| 16 | }; |
| 17 | |
| 18 | state = { |
| 19 | injections: [] |
| 20 | } |
| 21 | |
| 22 | componentWillMount() { |
| 23 | this.context.registerInjectable({ |
| 24 | injectionId, |
| 25 | injectableInstance: this, |
| 26 | receive: (elements) => this.consume(elements) |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | componentWillUnmount() { |
| 31 | this.context.removeInjectable({ |
| 32 | injectionId, |
| 33 | injectableInstance: this |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | consume = (elements) => { |
| 38 | if (elements.length !== this.state.injections.length || |
| 39 | containsUniq(this.state.injections, elements)) { |
| 40 | this.setState({ injections: elements }); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | render() { |
| 45 | const keyed = keyedElements(`injections`, this.state.injections); |
| 46 | |
| 47 | return ( |
| 48 | <VeinComponent |
| 49 | injections={keyed} |
| 50 | {...this.props} |
| 51 | /> |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return InjectableComponent; |
| 57 | }; |
| 58 | |
| 59 | export default Injectable; |
no outgoing calls