UNPKG

4.12 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.lazyLoadState = exports.registerLazyLoadHook = void 0;
4var coreservices_1 = require("../common/coreservices");
5/**
6 * A [[TransitionHookFn]] that performs lazy loading
7 *
8 * When entering a state "abc" which has a `lazyLoad` function defined:
9 * - Invoke the `lazyLoad` function (unless it is already in process)
10 * - Flag the hook function as "in process"
11 * - The function should return a promise (that resolves when lazy loading is complete)
12 * - Wait for the promise to settle
13 * - If the promise resolves to a [[LazyLoadResult]], then register those states
14 * - Flag the hook function as "not in process"
15 * - If the hook was successful
16 * - Remove the `lazyLoad` function from the state declaration
17 * - If all the hooks were successful
18 * - Retry the transition (by returning a TargetState)
19 *
20 * ```
21 * .state('abc', {
22 * component: 'fooComponent',
23 * lazyLoad: () => import('./fooComponent')
24 * });
25 * ```
26 *
27 * See [[StateDeclaration.lazyLoad]]
28 */
29var lazyLoadHook = function (transition) {
30 var router = transition.router;
31 function retryTransition() {
32 if (transition.originalTransition().options().source !== 'url') {
33 // The original transition was not triggered via url sync
34 // The lazy state should be loaded now, so re-try the original transition
35 var orig = transition.targetState();
36 return router.stateService.target(orig.identifier(), orig.params(), orig.options());
37 }
38 // The original transition was triggered via url sync
39 // Run the URL rules and find the best match
40 var $url = router.urlService;
41 var result = $url.match($url.parts());
42 var rule = result && result.rule;
43 // If the best match is a state, redirect the transition (instead
44 // of calling sync() which supersedes the current transition)
45 if (rule && rule.type === 'STATE') {
46 var state = rule.state;
47 var params = result.match;
48 return router.stateService.target(state, params, transition.options());
49 }
50 // No matching state found, so let .sync() choose the best non-state match/otherwise
51 router.urlService.sync();
52 }
53 var promises = transition
54 .entering()
55 .filter(function (state) { return !!state.$$state().lazyLoad; })
56 .map(function (state) { return lazyLoadState(transition, state); });
57 return coreservices_1.services.$q.all(promises).then(retryTransition);
58};
59exports.registerLazyLoadHook = function (transitionService) {
60 return transitionService.onBefore({ entering: function (state) { return !!state.lazyLoad; } }, lazyLoadHook);
61};
62/**
63 * Invokes a state's lazy load function
64 *
65 * @param transition a Transition context
66 * @param state the state to lazy load
67 * @returns A promise for the lazy load result
68 */
69function lazyLoadState(transition, state) {
70 var lazyLoadFn = state.$$state().lazyLoad;
71 // Store/get the lazy load promise on/from the hookfn so it doesn't get re-invoked
72 var promise = lazyLoadFn['_promise'];
73 if (!promise) {
74 var success = function (result) {
75 delete state.lazyLoad;
76 delete state.$$state().lazyLoad;
77 delete lazyLoadFn['_promise'];
78 return result;
79 };
80 var error = function (err) {
81 delete lazyLoadFn['_promise'];
82 return coreservices_1.services.$q.reject(err);
83 };
84 promise = lazyLoadFn['_promise'] = coreservices_1.services.$q
85 .when(lazyLoadFn(transition, state))
86 .then(updateStateRegistry)
87 .then(success, error);
88 }
89 /** Register any lazy loaded state definitions */
90 function updateStateRegistry(result) {
91 if (result && Array.isArray(result.states)) {
92 result.states.forEach(function (_state) { return transition.router.stateRegistry.register(_state); });
93 }
94 return result;
95 }
96 return promise;
97}
98exports.lazyLoadState = lazyLoadState;
99//# sourceMappingURL=lazyLoad.js.map
\No newline at end of file