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