1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
13 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
14 | s = arguments[i];
|
15 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
16 | t[p] = s[p];
|
17 | }
|
18 | return t;
|
19 | };
|
20 | Object.defineProperty(exports, "__esModule", { value: true });
|
21 | var React = require("react");
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function asyncComponent(_a) {
|
28 | var loader = _a.loader, Placeholder = _a.Placeholder;
|
29 | var Component = null;
|
30 | return (function (_super) {
|
31 | __extends(AsyncRouteComponent, _super);
|
32 | function AsyncRouteComponent(props) {
|
33 | var _this = _super.call(this, props) || this;
|
34 | _this.updateState = _this.updateState.bind(_this);
|
35 | _this.state = {
|
36 | Component: Component,
|
37 | };
|
38 | return _this;
|
39 | }
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | AsyncRouteComponent.load = function () {
|
46 | return loader().then(function (ResolvedComponent) {
|
47 | Component = ResolvedComponent.default || ResolvedComponent;
|
48 | });
|
49 | };
|
50 | AsyncRouteComponent.getInitialProps = function (ctx) {
|
51 |
|
52 | if (Component !== null) {
|
53 | return Component.getInitialProps
|
54 | ? Component.getInitialProps(ctx)
|
55 | : Promise.resolve(null);
|
56 | }
|
57 | };
|
58 | AsyncRouteComponent.prototype.componentWillMount = function () {
|
59 | AsyncRouteComponent.load().then(this.updateState);
|
60 | };
|
61 | AsyncRouteComponent.prototype.updateState = function () {
|
62 |
|
63 |
|
64 | if (this.state.Component !== Component) {
|
65 | this.setState({
|
66 | Component: Component,
|
67 | });
|
68 | }
|
69 | };
|
70 | AsyncRouteComponent.prototype.render = function () {
|
71 | var ComponentFromState = this.state.Component;
|
72 | if (ComponentFromState) {
|
73 | return React.createElement(ComponentFromState, __assign({}, this.props));
|
74 | }
|
75 | if (Placeholder) {
|
76 | return React.createElement(Placeholder, __assign({}, this.props));
|
77 | }
|
78 | return null;
|
79 | };
|
80 | return AsyncRouteComponent;
|
81 | }(React.Component));
|
82 | }
|
83 | exports.asyncComponent = asyncComponent;
|
84 |
|
\ | No newline at end of file |