(contextKey)
| 1 | import React, { PropTypes } from 'react' |
| 2 | |
| 3 | const createContextEmitter = (contextKey) => ( |
| 4 | |
| 5 | class Emitter extends React.Component { |
| 6 | |
| 7 | static propTypes = { |
| 8 | children: PropTypes.node, |
| 9 | value: PropTypes.any |
| 10 | } |
| 11 | |
| 12 | static childContextTypes = { |
| 13 | [contextKey]: PropTypes.object |
| 14 | } |
| 15 | |
| 16 | constructor() { |
| 17 | super() |
| 18 | this.subscribers = [] |
| 19 | } |
| 20 | |
| 21 | getChildContext() { |
| 22 | return { |
| 23 | [contextKey]: { |
| 24 | getInitialValue: () => { |
| 25 | return this.props.value |
| 26 | }, |
| 27 | |
| 28 | subscribe: (subscriber) => { |
| 29 | this.subscribers.push(subscriber) |
| 30 | return () => { |
| 31 | this.subscribers = this.subscribers.filter( |
| 32 | alleged => alleged !== subscriber |
| 33 | ) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | componentWillReceiveProps(nextProps) { |
| 41 | this.subscribers.forEach(f => f(nextProps.value)) |
| 42 | } |
| 43 | |
| 44 | render() { |
| 45 | return this.props.children |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | ) |
| 50 | |
| 51 | const createContextSubscriber = (contextKey) => ( |
| 52 |
no outgoing calls
no test coverage detected
searching dependent graphs…