async-import-react-component
Version:
Support react components import asynchronously, with support for code splitting
2 lines (1 loc) • 2.95 kB
JavaScript
import t,{Component as e}from"react";function n(t,e,n,r,o,u,i){try{var c=t[u](i),a=c.value}catch(t){return void n(t)}c.done?e(a):Promise.resolve(a).then(r,o)}function r(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function o(t){return(o=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function u(t,e){return(u=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function i(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t):e}function c(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,r=o(t);if(e){var u=o(this).constructor;n=Reflect.construct(r,arguments,u)}else n=r.apply(this,arguments);return i(this,n)}}export default function(o){var i,a=null,l=null,f=0,s=null;if("function"==typeof o)i=o;else{if("[object Object]"!==Object.prototype.toString.call(o))throw new Error("Please make sure the input `option` meets the specification.");if("function"!=typeof o.resolve)throw new TypeError('`resolve` must be a function like: () => import("./xxx").');i=o.resolve,a=void 0===o.loading?null:o.loading,l=void 0===o.error?null:o.error,f=void 0===o.delay?0:o.delay}return function(o){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&u(t,e)}(b,e);var p,y,h,v,m,d=c(b);function b(t){var e;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,b),(e=d.call(this,t)).state={ResultComponent:null,status:"loading"},e}return p=b,(y=[{key:"componentDidMount",value:(v=regeneratorRuntime.mark((function t(){var e,n,r=this;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,i();case 2:e=t.sent,(n=e.default)?s=setTimeout((function(){r.setState({ResultComponent:n,status:"normal"})}),f):this.setState({status:"error"});case 5:case"end":return t.stop()}}),t,this)})),m=function(){var t=this,e=arguments;return new Promise((function(r,o){var u=v.apply(t,e);function i(t){n(u,r,o,i,c,"next",t)}function c(t){n(u,r,o,i,c,"throw",t)}i(void 0)}))},function(){return m.apply(this,arguments)})},{key:"componentWillUnmount",value:function(){s&&clearTimeout(s),s=null}},{key:"render",value:function(){var e=this.state,n=e.status,r=e.ResultComponent;return"loading"===n?a:"error"===n?l:r?t.createElement(r,this.props):null}}])&&r(p.prototype,y),h&&r(p,h),b}()}