Dashboard sipadu mbip
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

implementation.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _react = require('react');
  4. var _react2 = _interopRequireDefault(_react);
  5. var _propTypes = require('prop-types');
  6. var _propTypes2 = _interopRequireDefault(_propTypes);
  7. var _gud = require('gud');
  8. var _gud2 = _interopRequireDefault(_gud);
  9. var _warning = require('warning');
  10. var _warning2 = _interopRequireDefault(_warning);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  14. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  15. var MAX_SIGNED_31_BIT_INT = 1073741823;
  16. // Inlined Object.is polyfill.
  17. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
  18. function objectIs(x, y) {
  19. if (x === y) {
  20. return x !== 0 || 1 / x === 1 / y;
  21. } else {
  22. return x !== x && y !== y;
  23. }
  24. }
  25. function createEventEmitter(value) {
  26. var handlers = [];
  27. return {
  28. on: function on(handler) {
  29. handlers.push(handler);
  30. },
  31. off: function off(handler) {
  32. handlers = handlers.filter(function (h) {
  33. return h !== handler;
  34. });
  35. },
  36. get: function get() {
  37. return value;
  38. },
  39. set: function set(newValue, changedBits) {
  40. value = newValue;
  41. handlers.forEach(function (handler) {
  42. return handler(value, changedBits);
  43. });
  44. }
  45. };
  46. }
  47. function onlyChild(children) {
  48. return Array.isArray(children) ? children[0] : children;
  49. }
  50. function createReactContext(defaultValue, calculateChangedBits) {
  51. var _Provider$childContex, _Consumer$contextType;
  52. var contextProp = '__create-react-context-' + (0, _gud2.default)() + '__';
  53. var Provider = function (_Component) {
  54. _inherits(Provider, _Component);
  55. function Provider() {
  56. var _temp, _this, _ret;
  57. _classCallCheck(this, Provider);
  58. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  59. args[_key] = arguments[_key];
  60. }
  61. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.emitter = createEventEmitter(_this.props.value), _temp), _possibleConstructorReturn(_this, _ret);
  62. }
  63. Provider.prototype.getChildContext = function getChildContext() {
  64. var _ref;
  65. return _ref = {}, _ref[contextProp] = this.emitter, _ref;
  66. };
  67. Provider.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  68. if (this.props.value !== nextProps.value) {
  69. var oldValue = this.props.value;
  70. var newValue = nextProps.value;
  71. var changedBits = void 0;
  72. if (objectIs(oldValue, newValue)) {
  73. changedBits = 0; // No change
  74. } else {
  75. changedBits = typeof calculateChangedBits === 'function' ? calculateChangedBits(oldValue, newValue) : MAX_SIGNED_31_BIT_INT;
  76. if (process.env.NODE_ENV !== 'production') {
  77. (0, _warning2.default)((changedBits & MAX_SIGNED_31_BIT_INT) === changedBits, 'calculateChangedBits: Expected the return value to be a ' + '31-bit integer. Instead received: %s', changedBits);
  78. }
  79. changedBits |= 0;
  80. if (changedBits !== 0) {
  81. this.emitter.set(nextProps.value, changedBits);
  82. }
  83. }
  84. }
  85. };
  86. Provider.prototype.render = function render() {
  87. return this.props.children;
  88. };
  89. return Provider;
  90. }(_react.Component);
  91. Provider.childContextTypes = (_Provider$childContex = {}, _Provider$childContex[contextProp] = _propTypes2.default.object.isRequired, _Provider$childContex);
  92. var Consumer = function (_Component2) {
  93. _inherits(Consumer, _Component2);
  94. function Consumer() {
  95. var _temp2, _this2, _ret2;
  96. _classCallCheck(this, Consumer);
  97. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  98. args[_key2] = arguments[_key2];
  99. }
  100. return _ret2 = (_temp2 = (_this2 = _possibleConstructorReturn(this, _Component2.call.apply(_Component2, [this].concat(args))), _this2), _this2.state = {
  101. value: _this2.getValue()
  102. }, _this2.onUpdate = function (newValue, changedBits) {
  103. var observedBits = _this2.observedBits | 0;
  104. if ((observedBits & changedBits) !== 0) {
  105. _this2.setState({ value: _this2.getValue() });
  106. }
  107. }, _temp2), _possibleConstructorReturn(_this2, _ret2);
  108. }
  109. Consumer.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  110. var observedBits = nextProps.observedBits;
  111. this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
  112. : observedBits;
  113. };
  114. Consumer.prototype.componentDidMount = function componentDidMount() {
  115. if (this.context[contextProp]) {
  116. this.context[contextProp].on(this.onUpdate);
  117. }
  118. var observedBits = this.props.observedBits;
  119. this.observedBits = observedBits === undefined || observedBits === null ? MAX_SIGNED_31_BIT_INT // Subscribe to all changes by default
  120. : observedBits;
  121. };
  122. Consumer.prototype.componentWillUnmount = function componentWillUnmount() {
  123. if (this.context[contextProp]) {
  124. this.context[contextProp].off(this.onUpdate);
  125. }
  126. };
  127. Consumer.prototype.getValue = function getValue() {
  128. if (this.context[contextProp]) {
  129. return this.context[contextProp].get();
  130. } else {
  131. return defaultValue;
  132. }
  133. };
  134. Consumer.prototype.render = function render() {
  135. return onlyChild(this.props.children)(this.state.value);
  136. };
  137. return Consumer;
  138. }(_react.Component);
  139. Consumer.contextTypes = (_Consumer$contextType = {}, _Consumer$contextType[contextProp] = _propTypes2.default.object, _Consumer$contextType);
  140. return {
  141. Provider: Provider,
  142. Consumer: Consumer
  143. };
  144. }
  145. exports.default = createReactContext;
  146. module.exports = exports['default'];