react-redux-isomorphic
Version:
Set of utilities for creating isomorphic applications using react-redux
179 lines (148 loc) • 5.67 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { createElement, Component } from 'react';
import inject from './inject';
import { LoadContextError } from './errors';
export var ContextResolver = /*#__PURE__*/function (_Component) {
_inherits(ContextResolver, _Component);
var _super = _createSuper(ContextResolver);
function ContextResolver(props) {
var _this;
_classCallCheck(this, ContextResolver);
_this = _super.call(this, props);
_this.init();
return _this;
}
_createClass(ContextResolver, [{
key: "componentDidUpdate",
value: function () {
var _componentDidUpdate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(oldProps) {
var _this$props, shouldReload, componentProps;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_this$props = this.props, shouldReload = _this$props.shouldReload, componentProps = _this$props.componentProps;
if (!(shouldReload && shouldReload(componentProps, oldProps.componentProps))) {
_context.next = 6;
break;
}
_context.next = 4;
return this.destroy();
case 4:
_context.next = 6;
return this.init();
case 6:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function componentDidUpdate(_x) {
return _componentDidUpdate.apply(this, arguments);
}
return componentDidUpdate;
}()
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.destroy();
}
}, {
key: "init",
value: function init() {
var _this$props2 = this.props,
isReady = _this$props2.isomorphic.isReady,
isomorphicId = _this$props2.isomorphicId,
loadContext = _this$props2.loadContext;
if (!isReady) {
loadContext(isomorphicId);
}
return this.requestContext();
}
}, {
key: "requestContext",
value: function () {
var _requestContext = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
var _this$props3, componentProps, isomorphicId, isReady, loadParams, getContext, loadContextSuccess, loadContextError, error, context;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_this$props3 = this.props, componentProps = _this$props3.componentProps, isomorphicId = _this$props3.isomorphicId, isReady = _this$props3.isomorphic.isReady, loadParams = _this$props3.loadParams, getContext = _this$props3.getContext, loadContextSuccess = _this$props3.loadContextSuccess, loadContextError = _this$props3.loadContextError;
if (!isReady) {
_context2.next = 3;
break;
}
return _context2.abrupt("return");
case 3:
_context2.prev = 3;
_context2.next = 6;
return getContext(loadParams, componentProps);
case 6:
context = _context2.sent;
loadContextSuccess(isomorphicId, context);
_context2.next = 13;
break;
case 10:
_context2.prev = 10;
_context2.t0 = _context2["catch"](3);
error = _context2.t0;
case 13:
if (error) {
_context2.next = 15;
break;
}
return _context2.abrupt("return");
case 15:
if (!(error instanceof LoadContextError)) {
_context2.next = 19;
break;
}
loadContextError(isomorphicId, error.error);
_context2.next = 20;
break;
case 19:
throw error;
case 20:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[3, 10]]);
}));
function requestContext() {
return _requestContext.apply(this, arguments);
}
return requestContext;
}()
}, {
key: "destroy",
value: function destroy() {
var _this$props4 = this.props,
isomorphicId = _this$props4.isomorphicId,
destroy = _this$props4.destroy;
destroy(isomorphicId);
}
}, {
key: "render",
value: function render() {
var _this$props5 = this.props,
isomorphic = _this$props5.isomorphic,
component = _this$props5.component,
componentProps = _this$props5.componentProps;
return /*#__PURE__*/createElement(component, _objectSpread(_objectSpread({}, componentProps), {}, {
isomorphic: isomorphic
}));
}
}]);
return ContextResolver;
}(Component);
var IsomorphicWrapper = inject(ContextResolver);
export default IsomorphicWrapper;