index.js 4.9 KB

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