UNPKG

6.79 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _react = require('react');
6
7var _react2 = _interopRequireDefault(_react);
8
9var _propTypes = require('prop-types');
10
11var _propTypes2 = _interopRequireDefault(_propTypes);
12
13var _gud = require('gud');
14
15var _gud2 = _interopRequireDefault(_gud);
16
17var _warning = require('warning');
18
19var _warning2 = _interopRequireDefault(_warning);
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24
25function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
26
27function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
28
29var MAX_SIGNED_31_BIT_INT = 1073741823;
30
31// Inlined Object.is polyfill.
32// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
33function objectIs(x, y) {
34 if (x === y) {
35 return x !== 0 || 1 / x === 1 / y;
36 } else {
37 return x !== x && y !== y;
38 }
39}
40
41function createEventEmitter(value) {
42 var handlers = [];
43 return {
44 on: function on(handler) {
45 handlers.push(handler);
46 },
47 off: function off(handler) {
48 handlers = handlers.filter(function (h) {
49 return h !== handler;
50 });
51 },
52 get: function get() {
53 return value;
54 },
55 set: function set(newValue, changedBits) {
56 value = newValue;
57 handlers.forEach(function (handler) {
58 return handler(value, changedBits);
59 });
60 }
61 };
62}
63
64function onlyChild(children) {
65 return Array.isArray(children) ? children[0] : children;
66}
67
68function createReactContext(defaultValue, calculateChangedBits) {
69 var _Provider$childContex, _Consumer$contextType;
70
71 var contextProp = '__create-react-context-' + (0, _gud2.default)() + '__';
72
73 var Provider = function (_Component) {
74 _inherits(Provider, _Component);
75
76 function Provider() {
77 var _temp, _this, _ret;
78
79 _classCallCheck(this, Provider);
80
81 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
82 args[_key] = arguments[_key];
83 }
84
85 return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.emitter = createEventEmitter(_this.props.value), _temp), _possibleConstructorReturn(_this, _ret);
86 }
87
88 Provider.prototype.getChildContext = function getChildContext() {
89 var _ref;
90
91 return _ref = {}, _ref[contextProp] = this.emitter, _ref;
92 };
93
94 Provider.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
95 if (this.props.value !== nextProps.value) {
96 var oldValue = this.props.value;
97 var newValue = nextProps.value;
98 var changedBits = void 0;
99
100 if (objectIs(oldValue, newValue)) {
101 changedBits = 0; // No change
102 } else {
103 changedBits = typeof calculateChangedBits === 'function' ? calculateChangedBits(oldValue, newValue) : MAX_SIGNED_31_BIT_INT;
104 if (process.env.NODE_ENV !== 'production') {
105 (0, _warning2.default)((changedBits & MAX_SIGNED_31_BIT_INT) === changedBits, 'calculateChangedBits: Expected the return value to be a ' + '31-bit integer. Instead received: %s', changedBits);
106 }
107
108 changedBits |= 0;
109
110 if (changedBits !== 0) {
111 this.emitter.set(nextProps.value, changedBits);
112 }
113 }
114 }
115 };
116
117 Provider.prototype.render = function render() {
118 return this.props.children;
119 };
120
121 return Provider;
122 }(_react.Component);
123
124 Provider.childContextTypes = (_Provider$childContex = {}, _Provider$childContex[contextProp] = _propTypes2.default.object.isRequired, _Provider$childContex);
125
126 var Consumer = function (_Component2) {
127 _inherits(Consumer, _Component2);
128
129 function Consumer() {
130 var _temp2, _this2, _ret2;
131
132 _classCallCheck(this, Consumer);
133
134 for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
135 args[_key2] = arguments[_key2];
136 }
137
138 return _ret2 = (_temp2 = (_this2 = _possibleConstructorReturn(this, _Component2.call.apply(_Component2, [this].concat(args))), _this2), _this2.state = {
139 value: _this2.getValue()
140 }, _this2.onUpdate = function (newValue, changedBits) {
141 var observedBits = _this2.observedBits | 0;
142 if ((observedBits & changedBits) !== 0) {
143 _this2.setState({ value: _this2.getValue() });
144 }
145 }, _temp2), _possibleConstructorReturn(_this2, _ret2);
146 }
147
148 Consumer.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
149 var observedBits = nextProps.observedBits;
150
151 this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
152 : observedBits;
153 };
154
155 Consumer.prototype.componentDidMount = function componentDidMount() {
156 if (this.context[contextProp]) {
157 this.context[contextProp].on(this.onUpdate);
158 }
159 var observedBits = this.props.observedBits;
160
161 this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
162 : observedBits;
163 };
164
165 Consumer.prototype.componentWillUnmount = function componentWillUnmount() {
166 if (this.context[contextProp]) {
167 this.context[contextProp].off(this.onUpdate);
168 }
169 };
170
171 Consumer.prototype.getValue = function getValue() {
172 if (this.context[contextProp]) {
173 return this.context[contextProp].get();
174 } else {
175 return defaultValue;
176 }
177 };
178
179 Consumer.prototype.render = function render() {
180 return onlyChild(this.props.children)(this.state.value);
181 };
182
183 return Consumer;
184 }(_react.Component);
185
186 Consumer.contextTypes = (_Consumer$contextType = {}, _Consumer$contextType[contextProp] = _propTypes2.default.object, _Consumer$contextType);
187
188
189 return {
190 Provider: Provider,
191 Consumer: Consumer
192 };
193}
194
195exports.default = createReactContext;
196module.exports = exports['default'];
\No newline at end of file