( defaultValue: T, calculateChangedBits: ?(a: T, b: T) => number )
| 67 | } |
| 68 | |
| 69 | function createReactContext<T>( |
| 70 | defaultValue: T, |
| 71 | calculateChangedBits: ?(a: T, b: T) => number |
| 72 | ): Context<T> { |
| 73 | const contextProp = '__create-react-context-' + gud() + '__'; |
| 74 | |
| 75 | class Provider extends Component<ProviderProps<T>> { |
| 76 | emitter = createEventEmitter(this.props.value); |
| 77 | |
| 78 | static childContextTypes = { |
| 79 | [contextProp]: PropTypes.object.isRequired |
| 80 | }; |
| 81 | |
| 82 | getChildContext() { |
| 83 | return { |
| 84 | [contextProp]: this.emitter |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | componentWillReceiveProps(nextProps) { |
| 89 | if (this.props.value !== nextProps.value) { |
| 90 | let oldValue = this.props.value; |
| 91 | let newValue = nextProps.value; |
| 92 | let changedBits: number; |
| 93 | |
| 94 | if (objectIs(oldValue, newValue)) { |
| 95 | changedBits = 0; // No change |
| 96 | } else { |
| 97 | changedBits = |
| 98 | typeof calculateChangedBits === 'function' |
| 99 | ? calculateChangedBits(oldValue, newValue) |
| 100 | : MAX_SIGNED_31_BIT_INT; |
| 101 | if (process.env.NODE_ENV !== 'production') { |
| 102 | warning( |
| 103 | (changedBits & MAX_SIGNED_31_BIT_INT) === changedBits, |
| 104 | 'calculateChangedBits: Expected the return value to be a ' + |
| 105 | '31-bit integer. Instead received: %s', |
| 106 | changedBits |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | changedBits |= 0; |
| 111 | |
| 112 | if (changedBits !== 0) { |
| 113 | this.emitter.set(nextProps.value, changedBits); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | render() { |
| 120 | return this.props.children; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | class Consumer extends Component<ConsumerProps<T>, ConsumerState<T>> { |
| 125 | static contextTypes = { |
| 126 | [contextProp]: PropTypes.object |
no outgoing calls
no test coverage detected
searching dependent graphs…