1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var detectBackend_1 = require("../utils/detectBackend");
|
4 | var signatures_1 = require("../utils/signatures");
|
5 | var marks_1 = require("./marks");
|
6 | var pending_1 = require("./pending");
|
7 | var preloaders_1 = require("./preloaders");
|
8 | var registry_1 = require("./registry");
|
9 | var utils_1 = require("./utils");
|
10 | function toLoadable(firstImportFunction, autoImport) {
|
11 | if (autoImport === void 0) { autoImport = true; }
|
12 | var importFunction = firstImportFunction;
|
13 | var loadImportedComponent = function () {
|
14 | return Promise.all([importFunction()].concat(preloaders_1.getPreloaders())).then(function (_a) {
|
15 | var result = _a[0];
|
16 | return result;
|
17 | });
|
18 | };
|
19 | var functionSignature = signatures_1.getFunctionSignature(importFunction);
|
20 | var mark = signatures_1.importMatch(functionSignature);
|
21 | var resolveResolution;
|
22 | var resolution = new Promise(function (r) {
|
23 | resolveResolution = r;
|
24 | });
|
25 | var loadable = {
|
26 |
|
27 | mark: mark,
|
28 | resolution: resolution,
|
29 | done: false,
|
30 | ok: false,
|
31 | error: null,
|
32 | payload: undefined,
|
33 | promise: undefined,
|
34 | isLoading: function () {
|
35 | return !!this.promise && !this.done;
|
36 | },
|
37 | reset: function () {
|
38 | this.done = false;
|
39 | this.ok = true;
|
40 | this.payload = undefined;
|
41 | this.promise = undefined;
|
42 | },
|
43 | replaceImportFunction: function (newImportFunction) {
|
44 | importFunction = newImportFunction;
|
45 | },
|
46 | get importer() {
|
47 | return importFunction;
|
48 | },
|
49 | then: function (cb, err) {
|
50 | if (this.promise) {
|
51 | return this.promise.then(cb, err);
|
52 | }
|
53 | if (err) {
|
54 | err();
|
55 | }
|
56 | return Promise.reject();
|
57 | },
|
58 | loadIfNeeded: function () {
|
59 | if (this.error) {
|
60 | this.reset();
|
61 | }
|
62 | if (!this.promise) {
|
63 | this.load();
|
64 | }
|
65 | return this.promise;
|
66 | },
|
67 | tryResolveSync: function (then) {
|
68 | if (this.done) {
|
69 | var result_1 = then(this.payload);
|
70 | return {
|
71 | then: function (cb) {
|
72 |
|
73 | cb(result_1);
|
74 | return Promise.resolve(result_1);
|
75 | },
|
76 | };
|
77 | }
|
78 | return this.loadIfNeeded().then(then);
|
79 | },
|
80 | reload: function () {
|
81 | if (this.promise) {
|
82 | this.promise = undefined;
|
83 | return this.load();
|
84 | }
|
85 | return Promise.resolve();
|
86 | },
|
87 | _probeChanges: function () {
|
88 | var _this = this;
|
89 | return Promise.resolve(importFunction())
|
90 | .then(function (payload) { return payload !== _this.payload; })
|
91 | .catch(function (err) {
|
92 | throw err;
|
93 | });
|
94 | },
|
95 | load: function () {
|
96 | var _this = this;
|
97 | if (!this.promise) {
|
98 | var promise_1 = (this.promise = loadImportedComponent().then(function (payload) {
|
99 | _this.done = true;
|
100 | _this.ok = true;
|
101 | _this.payload = payload;
|
102 | _this.error = null;
|
103 | pending_1.removeFromPending(promise_1);
|
104 | resolveResolution(payload);
|
105 | return payload;
|
106 | }, function (err) {
|
107 | _this.done = true;
|
108 | _this.ok = false;
|
109 | _this.error = err;
|
110 | pending_1.removeFromPending(promise_1);
|
111 | throw err;
|
112 | }));
|
113 | pending_1.addPending(promise_1);
|
114 | }
|
115 | return this.promise;
|
116 | },
|
117 | };
|
118 | if (mark && mark.length) {
|
119 | registry_1.LOADABLE_SIGNATURE.set(utils_1.toKnownSignature(functionSignature, mark), loadable);
|
120 | marks_1.assignLoadableMark(mark, loadable);
|
121 | }
|
122 | else {
|
123 | if (process.env.NODE_ENV !== 'development') {
|
124 |
|
125 | console.warn('react-imported-component: no mark found at', importFunction, 'Please check babel plugin or macro setup, as well as imported-component\'s limitations. See https://github.com/theKashey/react-imported-component/issues/147');
|
126 | }
|
127 | }
|
128 |
|
129 | if (detectBackend_1.isBackend && autoImport) {
|
130 | loadable.load();
|
131 | }
|
132 | return loadable;
|
133 | }
|
134 | exports.toLoadable = toLoadable;
|