react.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  6. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  7. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  8. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  9. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  10. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  11. import React, { PureComponent } from 'react'; // eslint-disable-line import/no-unresolved
  12. export var PersistGate =
  13. /*#__PURE__*/
  14. function (_PureComponent) {
  15. _inherits(PersistGate, _PureComponent);
  16. function PersistGate() {
  17. var _getPrototypeOf2;
  18. var _this;
  19. _classCallCheck(this, PersistGate);
  20. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  21. args[_key] = arguments[_key];
  22. }
  23. _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(PersistGate)).call.apply(_getPrototypeOf2, [this].concat(args)));
  24. _defineProperty(_assertThisInitialized(_this), "state", {
  25. bootstrapped: false
  26. });
  27. _defineProperty(_assertThisInitialized(_this), "_unsubscribe", void 0);
  28. _defineProperty(_assertThisInitialized(_this), "handlePersistorState", function () {
  29. var persistor = _this.props.persistor;
  30. var _persistor$getState = persistor.getState(),
  31. bootstrapped = _persistor$getState.bootstrapped;
  32. if (bootstrapped) {
  33. if (_this.props.onBeforeLift) {
  34. Promise.resolve(_this.props.onBeforeLift()).finally(function () {
  35. return _this.setState({
  36. bootstrapped: true
  37. });
  38. });
  39. } else {
  40. _this.setState({
  41. bootstrapped: true
  42. });
  43. }
  44. _this._unsubscribe && _this._unsubscribe();
  45. }
  46. });
  47. return _this;
  48. }
  49. _createClass(PersistGate, [{
  50. key: "componentDidMount",
  51. value: function componentDidMount() {
  52. this._unsubscribe = this.props.persistor.subscribe(this.handlePersistorState);
  53. this.handlePersistorState();
  54. }
  55. }, {
  56. key: "componentWillUnmount",
  57. value: function componentWillUnmount() {
  58. this._unsubscribe && this._unsubscribe();
  59. }
  60. }, {
  61. key: "render",
  62. value: function render() {
  63. if (process.env.NODE_ENV !== 'production') {
  64. if (typeof this.props.children === 'function' && this.props.loading) console.error('redux-persist: PersistGate expects either a function child or loading prop, but not both. The loading prop will be ignored.');
  65. }
  66. if (typeof this.props.children === 'function') {
  67. return this.props.children(this.state.bootstrapped);
  68. }
  69. return this.state.bootstrapped ? this.props.children : this.props.loading;
  70. }
  71. }]);
  72. return PersistGate;
  73. }(PureComponent);
  74. _defineProperty(PersistGate, "defaultProps", {
  75. children: null,
  76. loading: null
  77. });