UNPKG

3.15 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 * @format
9 * @emails oncall+relay
10 */
11'use strict';
12
13var _require = require('./loadQuery'),
14 loadQuery = _require.loadQuery;
15
16function loadEntryPoint(environmentProvider, entryPoint, entryPointParams) {
17 // Start loading the code for the entrypoint
18 var loadingPromise = null;
19
20 if (entryPoint.root.getModuleIfRequired() == null) {
21 loadingPromise = entryPoint.root.load();
22 }
23
24 var preloadProps = entryPoint.getPreloadProps(entryPointParams);
25 var queries = preloadProps.queries,
26 entryPoints = preloadProps.entryPoints,
27 extraProps = preloadProps.extraProps;
28 var preloadedQueries = {};
29 var preloadedEntryPoints = {};
30
31 if (queries != null) {
32 var queriesPropNames = Object.keys(queries);
33 queriesPropNames.forEach(function (queryPropName) {
34 var _queries$queryPropNam = queries[queryPropName],
35 environmentProviderOptions = _queries$queryPropNam.environmentProviderOptions,
36 options = _queries$queryPropNam.options,
37 parameters = _queries$queryPropNam.parameters,
38 variables = _queries$queryPropNam.variables;
39 var environment = environmentProvider.getEnvironment(environmentProviderOptions);
40 preloadedQueries[queryPropName] = loadQuery(environment, parameters, variables, {
41 fetchPolicy: options === null || options === void 0 ? void 0 : options.fetchPolicy,
42 networkCacheConfig: options === null || options === void 0 ? void 0 : options.networkCacheConfig
43 }, environmentProviderOptions);
44 });
45 }
46
47 if (entryPoints != null) {
48 var entryPointPropNames = Object.keys(entryPoints);
49 entryPointPropNames.forEach(function (entryPointPropName) {
50 var entryPointDescription = entryPoints[entryPointPropName];
51
52 if (entryPointDescription == null) {
53 return;
54 }
55
56 var nestedEntryPoint = entryPointDescription.entryPoint,
57 nestedParams = entryPointDescription.entryPointParams;
58 preloadedEntryPoints[entryPointPropName] = loadEntryPoint(environmentProvider, nestedEntryPoint, nestedParams);
59 });
60 }
61
62 var dispose = function dispose() {
63 if (preloadedQueries != null) {
64 Object.values(preloadedQueries).forEach(function (_ref) {
65 var innerDispose = _ref.dispose;
66 innerDispose();
67 });
68 }
69 };
70
71 return {
72 dispose: dispose,
73 entryPoints: preloadedEntryPoints,
74 extraProps: extraProps !== null && extraProps !== void 0 ? extraProps : null,
75 getComponent: function getComponent() {
76 var component = entryPoint.root.getModuleIfRequired();
77
78 if (component == null) {
79 var _loadingPromise;
80
81 loadingPromise = (_loadingPromise = loadingPromise) !== null && _loadingPromise !== void 0 ? _loadingPromise : entryPoint.root.load();
82 throw loadingPromise;
83 } // $FlowFixMe[incompatible-cast] - trust me Flow, its entryPoint component
84
85
86 return component;
87 },
88 queries: preloadedQueries
89 };
90}
91
92module.exports = loadEntryPoint;
\No newline at end of file