You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
6.8 KiB
255 lines
6.8 KiB
'use strict';
|
|
|
|
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
|
|
var React = require('react');
|
|
var React__default = _interopDefault(React);
|
|
|
|
function _classCallCheck(instance, Constructor) {
|
|
if (!(instance instanceof Constructor)) {
|
|
throw new TypeError("Cannot call a class as a function");
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
function _createClass(Constructor, protoProps, staticProps) {
|
|
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
return Constructor;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
function _objectSpread(target) {
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
var source = arguments[i] != null ? arguments[i] : {};
|
|
var ownKeys = Object.keys(source);
|
|
|
|
if (typeof Object.getOwnPropertySymbols === 'function') {
|
|
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
|
|
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
}));
|
|
}
|
|
|
|
ownKeys.forEach(function (key) {
|
|
_defineProperty(target, key, source[key]);
|
|
});
|
|
}
|
|
|
|
return target;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
function _getPrototypeOf(o) {
|
|
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
return o.__proto__ || Object.getPrototypeOf(o);
|
|
};
|
|
return _getPrototypeOf(o);
|
|
}
|
|
|
|
function _setPrototypeOf(o, p) {
|
|
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
|
o.__proto__ = p;
|
|
return o;
|
|
};
|
|
|
|
return _setPrototypeOf(o, p);
|
|
}
|
|
|
|
function _assertThisInitialized(self) {
|
|
if (self === void 0) {
|
|
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
function _possibleConstructorReturn(self, call) {
|
|
if (call && (typeof call === "object" || typeof call === "function")) {
|
|
return call;
|
|
}
|
|
|
|
return _assertThisInitialized(self);
|
|
}
|
|
|
|
function _toConsumableArray(arr) {
|
|
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
|
|
}
|
|
|
|
function _arrayWithoutHoles(arr) {
|
|
if (Array.isArray(arr)) {
|
|
for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
|
|
|
|
return arr2;
|
|
}
|
|
}
|
|
|
|
function _iterableToArray(iter) {
|
|
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
|
|
}
|
|
|
|
function _nonIterableSpread() {
|
|
throw new TypeError("Invalid attempt to spread non-iterable instance");
|
|
}
|
|
|
|
var cached = {};
|
|
|
|
function registerModel(app, model) {
|
|
model = model.default || model;
|
|
|
|
if (!cached[model.namespace]) {
|
|
app.model(model);
|
|
cached[model.namespace] = 1;
|
|
}
|
|
}
|
|
|
|
var defaultLoadingComponent = function defaultLoadingComponent() {
|
|
return null;
|
|
};
|
|
|
|
function asyncComponent(config) {
|
|
var resolve = config.resolve;
|
|
return (
|
|
/*#__PURE__*/
|
|
function (_Component) {
|
|
_inherits(DynamicComponent, _Component);
|
|
|
|
function DynamicComponent() {
|
|
var _getPrototypeOf2;
|
|
|
|
var _this;
|
|
|
|
_classCallCheck(this, DynamicComponent);
|
|
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(DynamicComponent)).call.apply(_getPrototypeOf2, [this].concat(args)));
|
|
_this.LoadingComponent = config.LoadingComponent || defaultLoadingComponent;
|
|
_this.state = {
|
|
AsyncComponent: null
|
|
};
|
|
|
|
_this.load();
|
|
|
|
return _this;
|
|
}
|
|
|
|
_createClass(DynamicComponent, [{
|
|
key: "componentDidMount",
|
|
value: function componentDidMount() {
|
|
this.mounted = true;
|
|
}
|
|
}, {
|
|
key: "componentWillUnmount",
|
|
value: function componentWillUnmount() {
|
|
this.mounted = false;
|
|
}
|
|
}, {
|
|
key: "load",
|
|
value: function load() {
|
|
var _this2 = this;
|
|
|
|
resolve().then(function (m) {
|
|
var AsyncComponent = m.default || m;
|
|
|
|
if (_this2.mounted) {
|
|
_this2.setState({
|
|
AsyncComponent: AsyncComponent
|
|
});
|
|
} else {
|
|
_this2.state.AsyncComponent = AsyncComponent; // eslint-disable-line
|
|
}
|
|
});
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render() {
|
|
var AsyncComponent = this.state.AsyncComponent;
|
|
var LoadingComponent = this.LoadingComponent;
|
|
if (AsyncComponent) return React__default.createElement(AsyncComponent, this.props);
|
|
return React__default.createElement(LoadingComponent, this.props);
|
|
}
|
|
}]);
|
|
|
|
return DynamicComponent;
|
|
}(React.Component)
|
|
);
|
|
}
|
|
|
|
function dynamic(config) {
|
|
var app = config.app,
|
|
resolveModels = config.models,
|
|
resolveComponent = config.component;
|
|
return asyncComponent(_objectSpread({
|
|
resolve: config.resolve || function () {
|
|
var models = typeof resolveModels === 'function' ? resolveModels() : [];
|
|
var component = resolveComponent();
|
|
return new Promise(function (resolve) {
|
|
Promise.all([].concat(_toConsumableArray(models), [component])).then(function (ret) {
|
|
if (!models || !models.length) {
|
|
return resolve(ret[0]);
|
|
} else {
|
|
var len = models.length;
|
|
ret.slice(0, len).forEach(function (m) {
|
|
m = m.default || m;
|
|
|
|
if (!Array.isArray(m)) {
|
|
m = [m];
|
|
}
|
|
|
|
m.map(function (_) {
|
|
return registerModel(app, _);
|
|
});
|
|
});
|
|
resolve(ret[len]);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}, config));
|
|
}
|
|
|
|
dynamic.setDefaultLoadingComponent = function (LoadingComponent) {
|
|
defaultLoadingComponent = LoadingComponent;
|
|
};
|
|
|
|
module.exports = dynamic;
|