index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';var React=require('react'),_inheritsLoose=require('@babel/runtime/helpers/inheritsLoose'),PropTypes=require('prop-types'),warning=require('tiny-warning');function _interopDefaultLegacy(e){return e&&typeof e==='object'&&'default'in e?e:{'default':e}}var React__default=/*#__PURE__*/_interopDefaultLegacy(React);var _inheritsLoose__default=/*#__PURE__*/_interopDefaultLegacy(_inheritsLoose);var PropTypes__default=/*#__PURE__*/_interopDefaultLegacy(PropTypes);var warning__default=/*#__PURE__*/_interopDefaultLegacy(warning);var MAX_SIGNED_31_BIT_INT = 1073741823;
  2. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {};
  3. function getUniqueId() {
  4. var key = '__global_unique_id__';
  5. return commonjsGlobal[key] = (commonjsGlobal[key] || 0) + 1;
  6. }
  7. function objectIs(x, y) {
  8. if (x === y) {
  9. return x !== 0 || 1 / x === 1 / y;
  10. } else {
  11. return x !== x && y !== y;
  12. }
  13. }
  14. function createEventEmitter(value) {
  15. var handlers = [];
  16. return {
  17. on: function on(handler) {
  18. handlers.push(handler);
  19. },
  20. off: function off(handler) {
  21. handlers = handlers.filter(function (h) {
  22. return h !== handler;
  23. });
  24. },
  25. get: function get() {
  26. return value;
  27. },
  28. set: function set(newValue, changedBits) {
  29. value = newValue;
  30. handlers.forEach(function (handler) {
  31. return handler(value, changedBits);
  32. });
  33. }
  34. };
  35. }
  36. function onlyChild(children) {
  37. return Array.isArray(children) ? children[0] : children;
  38. }
  39. function createReactContext(defaultValue, calculateChangedBits) {
  40. var _Provider$childContex, _Consumer$contextType;
  41. var contextProp = '__create-react-context-' + getUniqueId() + '__';
  42. var Provider = /*#__PURE__*/function (_Component) {
  43. _inheritsLoose__default['default'](Provider, _Component);
  44. function Provider() {
  45. var _this;
  46. _this = _Component.apply(this, arguments) || this;
  47. _this.emitter = createEventEmitter(_this.props.value);
  48. return _this;
  49. }
  50. var _proto = Provider.prototype;
  51. _proto.getChildContext = function getChildContext() {
  52. var _ref;
  53. return _ref = {}, _ref[contextProp] = this.emitter, _ref;
  54. };
  55. _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  56. if (this.props.value !== nextProps.value) {
  57. var oldValue = this.props.value;
  58. var newValue = nextProps.value;
  59. var changedBits;
  60. if (objectIs(oldValue, newValue)) {
  61. changedBits = 0;
  62. } else {
  63. changedBits = typeof calculateChangedBits === 'function' ? calculateChangedBits(oldValue, newValue) : MAX_SIGNED_31_BIT_INT;
  64. if (process.env.NODE_ENV !== 'production') {
  65. warning__default['default']((changedBits & MAX_SIGNED_31_BIT_INT) === changedBits, 'calculateChangedBits: Expected the return value to be a ' + '31-bit integer. Instead received: ' + changedBits);
  66. }
  67. changedBits |= 0;
  68. if (changedBits !== 0) {
  69. this.emitter.set(nextProps.value, changedBits);
  70. }
  71. }
  72. }
  73. };
  74. _proto.render = function render() {
  75. return this.props.children;
  76. };
  77. return Provider;
  78. }(React.Component);
  79. Provider.childContextTypes = (_Provider$childContex = {}, _Provider$childContex[contextProp] = PropTypes__default['default'].object.isRequired, _Provider$childContex);
  80. var Consumer = /*#__PURE__*/function (_Component2) {
  81. _inheritsLoose__default['default'](Consumer, _Component2);
  82. function Consumer() {
  83. var _this2;
  84. _this2 = _Component2.apply(this, arguments) || this;
  85. _this2.state = {
  86. value: _this2.getValue()
  87. };
  88. _this2.onUpdate = function (newValue, changedBits) {
  89. var observedBits = _this2.observedBits | 0;
  90. if ((observedBits & changedBits) !== 0) {
  91. _this2.setState({
  92. value: _this2.getValue()
  93. });
  94. }
  95. };
  96. return _this2;
  97. }
  98. var _proto2 = Consumer.prototype;
  99. _proto2.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  100. var observedBits = nextProps.observedBits;
  101. this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT : observedBits;
  102. };
  103. _proto2.componentDidMount = function componentDidMount() {
  104. if (this.context[contextProp]) {
  105. this.context[contextProp].on(this.onUpdate);
  106. }
  107. var observedBits = this.props.observedBits;
  108. this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT : observedBits;
  109. };
  110. _proto2.componentWillUnmount = function componentWillUnmount() {
  111. if (this.context[contextProp]) {
  112. this.context[contextProp].off(this.onUpdate);
  113. }
  114. };
  115. _proto2.getValue = function getValue() {
  116. if (this.context[contextProp]) {
  117. return this.context[contextProp].get();
  118. } else {
  119. return defaultValue;
  120. }
  121. };
  122. _proto2.render = function render() {
  123. return onlyChild(this.props.children)(this.state.value);
  124. };
  125. return Consumer;
  126. }(React.Component);
  127. Consumer.contextTypes = (_Consumer$contextType = {}, _Consumer$contextType[contextProp] = PropTypes__default['default'].object, _Consumer$contextType);
  128. return {
  129. Provider: Provider,
  130. Consumer: Consumer
  131. };
  132. }var index = React__default['default'].createContext || createReactContext;module.exports=index;