index.js 4.9 KB

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