|
| 1 | +// MIT License |
| 2 | +// Copyright (c) 2019-present StringEpsilon <[email protected]> |
| 3 | +// Copyright (c) 2017-2019 James Kyle <[email protected]> |
| 4 | +// https://github.com/StringEpsilon/mini-create-react-context |
| 5 | +import React from "react"; |
| 6 | +import PropTypes from "prop-types"; |
| 7 | +import warning from "tiny-warning"; |
| 8 | + |
| 9 | +const MAX_SIGNED_31_BIT_INT = 1073741823; |
| 10 | + |
| 11 | +const commonjsGlobal = |
| 12 | + typeof globalThis !== "undefined" // 'global proper' |
| 13 | + ? // eslint-disable-next-line no-undef |
| 14 | + globalThis |
| 15 | + : typeof window !== "undefined" |
| 16 | + ? window // Browser |
| 17 | + : typeof global !== "undefined" |
| 18 | + ? global // node.js |
| 19 | + : {}; |
| 20 | + |
| 21 | +function getUniqueId() { |
| 22 | + let key = "__global_unique_id__"; |
| 23 | + return (commonjsGlobal[key] = (commonjsGlobal[key] || 0) + 1); |
| 24 | +} |
| 25 | + |
| 26 | +// Inlined Object.is polyfill. |
| 27 | +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is |
| 28 | +function objectIs(x, y) { |
| 29 | + if (x === y) { |
| 30 | + return x !== 0 || 1 / x === 1 / y; |
| 31 | + } else { |
| 32 | + // eslint-disable-next-line no-self-compare |
| 33 | + return x !== x && y !== y; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function createEventEmitter(value) { |
| 38 | + let handlers = []; |
| 39 | + return { |
| 40 | + on(handler) { |
| 41 | + handlers.push(handler); |
| 42 | + }, |
| 43 | + |
| 44 | + off(handler) { |
| 45 | + handlers = handlers.filter(h => h !== handler); |
| 46 | + }, |
| 47 | + |
| 48 | + get() { |
| 49 | + return value; |
| 50 | + }, |
| 51 | + |
| 52 | + set(newValue, changedBits) { |
| 53 | + value = newValue; |
| 54 | + handlers.forEach(handler => handler(value, changedBits)); |
| 55 | + } |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +function onlyChild(children) { |
| 60 | + return Array.isArray(children) ? children[0] : children; |
| 61 | +} |
| 62 | + |
| 63 | +export default function createReactContext(defaultValue, calculateChangedBits) { |
| 64 | + const contextProp = "__create-react-context-" + getUniqueId() + "__"; |
| 65 | + |
| 66 | + class Provider extends React.Component { |
| 67 | + emitter = createEventEmitter(this.props.value); |
| 68 | + |
| 69 | + static childContextTypes = { |
| 70 | + [contextProp]: PropTypes.object.isRequired |
| 71 | + }; |
| 72 | + |
| 73 | + getChildContext() { |
| 74 | + return { |
| 75 | + [contextProp]: this.emitter |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + componentWillReceiveProps(nextProps) { |
| 80 | + if (this.props.value !== nextProps.value) { |
| 81 | + let oldValue = this.props.value; |
| 82 | + let newValue = nextProps.value; |
| 83 | + let changedBits; |
| 84 | + |
| 85 | + if (objectIs(oldValue, newValue)) { |
| 86 | + changedBits = 0; // No change |
| 87 | + } else { |
| 88 | + changedBits = |
| 89 | + typeof calculateChangedBits === "function" |
| 90 | + ? calculateChangedBits(oldValue, newValue) |
| 91 | + : MAX_SIGNED_31_BIT_INT; |
| 92 | + if (process.env.NODE_ENV !== "production") { |
| 93 | + warning( |
| 94 | + (changedBits & MAX_SIGNED_31_BIT_INT) === changedBits, |
| 95 | + "calculateChangedBits: Expected the return value to be a " + |
| 96 | + "31-bit integer. Instead received: " + |
| 97 | + changedBits |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + changedBits |= 0; |
| 102 | + |
| 103 | + if (changedBits !== 0) { |
| 104 | + this.emitter.set(nextProps.value, changedBits); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + render() { |
| 111 | + return this.props.children; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + class Consumer extends React.Component { |
| 116 | + static contextTypes = { |
| 117 | + [contextProp]: PropTypes.object |
| 118 | + }; |
| 119 | + |
| 120 | + observedBits; |
| 121 | + |
| 122 | + state = { |
| 123 | + value: this.getValue() |
| 124 | + }; |
| 125 | + |
| 126 | + componentWillReceiveProps(nextProps) { |
| 127 | + let { observedBits } = nextProps; |
| 128 | + this.observedBits = |
| 129 | + observedBits === undefined || observedBits === null |
| 130 | + ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default |
| 131 | + : observedBits; |
| 132 | + } |
| 133 | + |
| 134 | + componentDidMount() { |
| 135 | + if (this.context[contextProp]) { |
| 136 | + this.context[contextProp].on(this.onUpdate); |
| 137 | + } |
| 138 | + let { observedBits } = this.props; |
| 139 | + this.observedBits = |
| 140 | + observedBits === undefined || observedBits === null |
| 141 | + ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default |
| 142 | + : observedBits; |
| 143 | + } |
| 144 | + |
| 145 | + componentWillUnmount() { |
| 146 | + if (this.context[contextProp]) { |
| 147 | + this.context[contextProp].off(this.onUpdate); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + getValue() { |
| 152 | + if (this.context[contextProp]) { |
| 153 | + return this.context[contextProp].get(); |
| 154 | + } else { |
| 155 | + return defaultValue; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + onUpdate = (newValue, changedBits) => { |
| 160 | + const observedBits = this.observedBits | 0; |
| 161 | + if ((observedBits & changedBits) !== 0) { |
| 162 | + this.setState({ value: this.getValue() }); |
| 163 | + } |
| 164 | + }; |
| 165 | + |
| 166 | + render() { |
| 167 | + return onlyChild(this.props.children)(this.state.value); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return { |
| 172 | + Provider, |
| 173 | + Consumer |
| 174 | + }; |
| 175 | +} |
0 commit comments