UNPKG

3.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.clearImportedCache = exports.getLoadable = exports.executeLoadable = exports.dryRender = void 0;
4var signatures_1 = require("../utils/signatures");
5var pending_1 = require("./pending");
6var registry_1 = require("./registry");
7var toLoadable_1 = require("./toLoadable");
8var utils_1 = require("./utils");
9/**
10 * try to perform a render and loads all chunks required for it
11 * @deprecated
12 */
13var dryRender = function (renderFunction) {
14 renderFunction();
15 return Promise.resolve().then(pending_1.done);
16};
17exports.dryRender = dryRender;
18function executeLoadable(importFunction) {
19 if ('resolution' in importFunction) {
20 return importFunction.reload();
21 }
22 else {
23 return importFunction();
24 }
25}
26exports.executeLoadable = executeLoadable;
27/**
28 * wraps an `import` function with a tracker
29 * @internal
30 * @param importFunction
31 */
32function getLoadable(importFunction) {
33 if ('resolution' in importFunction) {
34 return importFunction;
35 }
36 if (registry_1.LOADABLE_WEAK_SIGNATURE.has(importFunction)) {
37 return registry_1.LOADABLE_WEAK_SIGNATURE.get(importFunction);
38 }
39 var rawSignature = (0, signatures_1.getFunctionSignature)(importFunction);
40 var ownMark = (0, signatures_1.importMatch)(String(rawSignature));
41 // read cache signature
42 var functionSignature = (0, utils_1.toKnownSignature)(rawSignature, ownMark);
43 if (registry_1.LOADABLE_SIGNATURE.has(functionSignature)) {
44 // tslint:disable-next-line:no-shadowed-variable
45 var loadable_1 = registry_1.LOADABLE_SIGNATURE.get(functionSignature);
46 loadable_1.replaceImportFunction(importFunction);
47 return loadable_1;
48 }
49 if (ownMark) {
50 if (process.env.NODE_ENV !== 'production') {
51 if (ownMark.length > 1) {
52 // tslint:disable-next-line:no-console
53 console.warn('A function exposes multiple imports. Imported-Components cannot know which one will be used.', {
54 importFunction: importFunction,
55 });
56 }
57 }
58 registry_1.LOADABLE_SIGNATURE.forEach(function (_a) {
59 var mark = _a.mark, importer = _a.importer;
60 if (mark[0] === ownMark[1] && mark.join('|') === ownMark.join('|')) {
61 // tslint:disable-next-line:no-console
62 console.warn("Another loadable found for an existing mark. That's possible, but signatures must match (https://github.com/theKashey/react-imported-component/issues/192):", {
63 mark: mark,
64 knownImporter: importer,
65 currentImported: importFunction,
66 currentSignature: String(importFunction),
67 knownSignature: String(importer),
68 });
69 }
70 });
71 }
72 var loadable = (0, toLoadable_1.toLoadable)(importFunction);
73 registry_1.LOADABLE_WEAK_SIGNATURE.set(importFunction, loadable);
74 return loadable;
75}
76exports.getLoadable = getLoadable;
77/**
78 * Reset `importers` weak cache
79 * @internal
80 */
81var clearImportedCache = function () { return registry_1.LOADABLE_SIGNATURE.clear(); };
82exports.clearImportedCache = clearImportedCache;