UNPKG

2.5 kBJavaScriptView Raw
1import { getFunctionSignature, importMatch } from '../utils/signatures';
2import { done } from './pending';
3import { LOADABLE_SIGNATURE, LOADABLE_WEAK_SIGNATURE } from './registry';
4import { toLoadable } from './toLoadable';
5import { toKnownSignature } from './utils';
6/**
7 * try to perform a render and loads all chunks required for it
8 * @deprecated
9 */
10export var dryRender = function (renderFunction) {
11 renderFunction();
12 return Promise.resolve().then(done);
13};
14export function executeLoadable(importFunction) {
15 if ('resolution' in importFunction) {
16 return importFunction.reload();
17 }
18 else {
19 return importFunction();
20 }
21}
22/**
23 * wraps an `import` function with a tracker
24 * @internal
25 * @param importFunction
26 */
27export function getLoadable(importFunction) {
28 if ('resolution' in importFunction) {
29 return importFunction;
30 }
31 if (LOADABLE_WEAK_SIGNATURE.has(importFunction)) {
32 return LOADABLE_WEAK_SIGNATURE.get(importFunction);
33 }
34 var rawSignature = getFunctionSignature(importFunction);
35 var ownMark = importMatch(String(rawSignature));
36 // read cache signature
37 var functionSignature = toKnownSignature(rawSignature, ownMark);
38 if (LOADABLE_SIGNATURE.has(functionSignature)) {
39 // tslint:disable-next-line:no-shadowed-variable
40 var loadable_1 = LOADABLE_SIGNATURE.get(functionSignature);
41 loadable_1.replaceImportFunction(importFunction);
42 return loadable_1;
43 }
44 if (ownMark) {
45 LOADABLE_SIGNATURE.forEach(function (_a) {
46 var mark = _a.mark, importer = _a.importer;
47 if (mark[0] === ownMark[1] && mark.join('|') === ownMark.join('|')) {
48 // tslint:disable-next-line:no-console
49 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):', {
50 mark: mark,
51 knownImporter: importer,
52 currentImported: importFunction,
53 currentSignature: String(importFunction),
54 knownSignature: String(importer),
55 });
56 }
57 });
58 }
59 var loadable = toLoadable(importFunction);
60 LOADABLE_WEAK_SIGNATURE.set(importFunction, loadable);
61 return loadable;
62}
63/**
64 * Reset `importers` weak cache
65 * @internal
66 */
67export var clearImportedCache = function () { return LOADABLE_SIGNATURE.clear(); };