index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import React, { Component } from 'react';
  2. import _inheritsLoose from '@babel/runtime/helpers/inheritsLoose';
  3. import PropTypes from 'prop-types';
  4. import gud from 'gud';
  5. import warning from 'tiny-warning';
  6. var MAX_SIGNED_31_BIT_INT = 1073741823;
  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-' + gud() + '__';
  42. var Provider =
  43. /*#__PURE__*/
  44. function (_Component) {
  45. _inheritsLoose(Provider, _Component);
  46. function Provider() {
  47. var _this;
  48. _this = _Component.apply(this, arguments) || this;
  49. _this.emitter = createEventEmitter(_this.props.value);
  50. return _this;
  51. }
  52. var _proto = Provider.prototype;
  53. _proto.getChildContext = function getChildContext() {
  54. var _ref;
  55. return _ref = {}, _ref[contextProp] = this.emitter, _ref;
  56. };
  57. _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  58. if (this.props.value !== nextProps.value) {
  59. var oldValue = this.props.value;
  60. var newValue = nextProps.value;
  61. var changedBits;
  62. if (objectIs(oldValue, newValue)) {
  63. changedBits = 0;
  64. } else {
  65. changedBits = typeof calculateChangedBits === 'function' ? calculateChangedBits(oldValue, newValue) : MAX_SIGNED_31_BIT_INT;
  66. if (process.env.NODE_ENV !== 'production') {
  67. warning((changedBits & MAX_SIGNED_31_BIT_INT) === changedBits, 'calculateChangedBits: Expected the return value to be a ' + '31-bit integer. Instead received: ' + changedBits);
  68. }
  69. changedBits |= 0;
  70. if (changedBits !== 0) {
  71. this.emitter.set(nextProps.value, changedBits);
  72. }
  73. }
  74. }
  75. };
  76. _proto.render = function render() {
  77. return this.props.children;
  78. };
  79. return Provider;
  80. }(Component);
  81. Provider.childContextTypes = (_Provider$childContex = {}, _Provider$childContex[contextProp] = PropTypes.object.isRequired, _Provider$childContex);
  82. var Consumer =
  83. /*#__PURE__*/
  84. 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;