123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import React, { Component } from 'react';
- import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
- import PropTypes from 'prop-types';
- import warning from 'tiny-warning';
- var MAX_SIGNED_31_BIT_INT = 1073741823;
- var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {};
- function getUniqueId() {
- var key = '__global_unique_id__';
- return commonjsGlobal[key] = (commonjsGlobal[key] || 0) + 1;
- }
- function objectIs(x, y) {
- if (x === y) {
- return x !== 0 || 1 / x === 1 / y;
- } else {
- return x !== x && y !== y;
- }
- }
- function createEventEmitter(value) {
- var handlers = [];
- return {
- on: function on(handler) {
- handlers.push(handler);
- },
- off: function off(handler) {
- handlers = handlers.filter(function (h) {
- return h !== handler;
- });
- },
- get: function get() {
- return value;
- },
- set: function set(newValue, changedBits) {
- value = newValue;
- handlers.forEach(function (handler) {
- return handler(value, changedBits);
- });
- }
- };
- }
- function onlyChild(children) {
- return Array.isArray(children) ? children[0] : children;
- }
- function createReactContext(defaultValue, calculateChangedBits) {
- var _Provider$childContex, _Consumer$contextType;
- var contextProp = '__create-react-context-' + getUniqueId() + '__';
- var Provider = /*#__PURE__*/function (_Component) {
- _inheritsLoose(Provider, _Component);
- function Provider() {
- var _this;
- _this = _Component.apply(this, arguments) || this;
- _this.emitter = createEventEmitter(_this.props.value);
- return _this;
- }
- var _proto = Provider.prototype;
- _proto.getChildContext = function getChildContext() {
- var _ref;
- return _ref = {}, _ref[contextProp] = this.emitter, _ref;
- };
- _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
- if (this.props.value !== nextProps.value) {
- var oldValue = this.props.value;
- var newValue = nextProps.value;
- var changedBits;
- if (objectIs(oldValue, newValue)) {
- changedBits = 0;
- } else {
- changedBits = typeof calculateChangedBits === 'function' ? calculateChangedBits(oldValue, newValue) : MAX_SIGNED_31_BIT_INT;
- if (process.env.NODE_ENV !== 'production') {
- warning((changedBits & MAX_SIGNED_31_BIT_INT) === changedBits, 'calculateChangedBits: Expected the return value to be a ' + '31-bit integer. Instead received: ' + changedBits);
- }
- changedBits |= 0;
- if (changedBits !== 0) {
- this.emitter.set(nextProps.value, changedBits);
- }
- }
- }
- };
- _proto.render = function render() {
- return this.props.children;
- };
- return Provider;
- }(Component);
- Provider.childContextTypes = (_Provider$childContex = {}, _Provider$childContex[contextProp] = PropTypes.object.isRequired, _Provider$childContex);
- var Consumer = /*#__PURE__*/function (_Component2) {
- _inheritsLoose(Consumer, _Component2);
- function Consumer() {
- var _this2;
- _this2 = _Component2.apply(this, arguments) || this;
- _this2.state = {
- value: _this2.getValue()
- };
- _this2.onUpdate = function (newValue, changedBits) {
- var observedBits = _this2.observedBits | 0;
- if ((observedBits & changedBits) !== 0) {
- _this2.setState({
- value: _this2.getValue()
- });
- }
- };
- return _this2;
- }
- var _proto2 = Consumer.prototype;
- _proto2.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
- var observedBits = nextProps.observedBits;
- this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT : observedBits;
- };
- _proto2.componentDidMount = function componentDidMount() {
- if (this.context[contextProp]) {
- this.context[contextProp].on(this.onUpdate);
- }
- var observedBits = this.props.observedBits;
- this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT : observedBits;
- };
- _proto2.componentWillUnmount = function componentWillUnmount() {
- if (this.context[contextProp]) {
- this.context[contextProp].off(this.onUpdate);
- }
- };
- _proto2.getValue = function getValue() {
- if (this.context[contextProp]) {
- return this.context[contextProp].get();
- } else {
- return defaultValue;
- }
- };
- _proto2.render = function render() {
- return onlyChild(this.props.children)(this.state.value);
- };
- return Consumer;
- }(Component);
- Consumer.contextTypes = (_Consumer$contextType = {}, _Consumer$contextType[contextProp] = PropTypes.object, _Consumer$contextType);
- return {
- Provider: Provider,
- Consumer: Consumer
- };
- }
- var index = React.createContext || createReactContext;
- export default index;
|