UNPKG

6.68 kBTypeScriptView Raw
1/**
2 * # The Resolve subsystem
3 *
4 * This subsystem is an asynchronous, hierarchical Dependency Injection system.
5 *
6 * Typically, resolve is configured on a state using a [[StateDeclaration.resolve]] declaration.
7 *
8 * @packageDocumentation
9 */
10/**
11 * An interface which is similar to an Angular 2 `Provider`
12 */
13export interface ProviderLike {
14 provide: any;
15 useClass?: any;
16 useFactory?: Function;
17 useValue?: any;
18 useExisting?: any;
19 deps?: any[];
20}
21/**
22 * A plain object used to describe a [[Resolvable]]
23 *
24 * These objects may be used in the [[StateDeclaration.resolve]] array to declare
25 * async data that the state or substates require.
26 *
27 * #### Example:
28 * ```js
29 *
30 * var state = {
31 * name: 'main',
32 * resolve: [
33 * { token: 'myData', deps: [MyDataApi], resolveFn: (myDataApi) => myDataApi.getData() },
34 * ],
35 * }
36 * ```
37 */
38export interface ResolvableLiteral {
39 /**
40 * A Dependency Injection token
41 *
42 * This Resolvable's DI token.
43 * The Resolvable will be injectable elsewhere using the token.
44 */
45 token: any;
46 /**
47 * A function which fetches the Resolvable's data
48 *
49 * A function which returns one of:
50 *
51 * - The resolved value (synchronously)
52 * - A promise for the resolved value
53 * - An Observable of the resolved value(s)
54 *
55 * This function will be provided the dependencies listed in [[deps]] as its arguments.
56 * The resolve system will asynchronously fetch the dependencies before invoking this function.
57 */
58 resolveFn: Function;
59 /**
60 * Defines the Resolve Policy
61 *
62 * A policy that defines when to invoke the resolve,
63 * and whether to wait for async and unwrap the data
64 */
65 policy?: ResolvePolicy;
66 /**
67 * The Dependency Injection tokens
68 *
69 * This is an array of Dependency Injection tokens for the dependencies of the [[resolveFn]].
70 *
71 * The DI tokens are references to other `Resolvables`, or to other
72 * services from the native DI system.
73 */
74 deps?: any[];
75 /** Pre-resolved data. */
76 data?: any;
77}
78/**
79 * Defines how a resolve is processed during a transition
80 *
81 * This object is the [[StateDeclaration.resolvePolicy]] property.
82 *
83 * #### Example:
84 * ```js
85 * // Fetched when the resolve's state is being entered.
86 * // Wait for the promise to resolve.
87 * var policy1 = { when: "LAZY", async: "WAIT" }
88 *
89 * // Fetched when the Transition is starting.
90 * // Do not wait for the returned promise to resolve.
91 * // Inject the raw promise/value
92 * var policy2 = { when: "EAGER", async: "NOWAIT" }
93 * ```
94 *
95 * The policy for a given Resolvable is merged from three sources (highest priority first):
96 *
97 * - 1) Individual resolve definition
98 * - 2) State definition
99 * - 3) Global default
100 *
101 * #### Example:
102 * ```js
103 * // Wait for an Observable to emit one item.
104 * // Since `wait` is not specified, it uses the `wait`
105 * // policy defined on the state, or the global default
106 * // if no `wait` policy is defined on the state
107 * import { RXWAIT } from '@uirouter/rx';
108 *
109 * var myResolvablePolicy = { async: RXWAIT }
110 * ```
111 */
112export interface ResolvePolicy {
113 /**
114 * Defines when a Resolvable is resolved (fetched) during a transition
115 *
116 * - `LAZY` (default)
117 * - Resolved as the resolve's state is being entered
118 * - `EAGER`
119 * - Resolved as the transition is starting
120 *
121 * #### Example:
122 * Resolves for `main` and `main.home` are fetched when each state is entered.
123 * All of `main` resolves are processed before fetching `main.home` resolves.
124 * ```js
125 * var state = {
126 * name: 'main',
127 * resolve: mainResolves, // defined elsewhere
128 * resolvePolicy: { when: 'LAZY' }, // default
129 * }
130 *
131 * var state = {
132 * name: 'main.home',
133 * resolve: homeResolves, // defined elsewhere
134 * resolvePolicy: { when: 'LAZY' }, // default
135 * }
136 * ```
137 *
138 * #### Example:
139 * Resolves for `main` and `main.home` are fetched at the same time when the transition starts.
140 * This happens earlier in the lifecycle than when states are entered.
141 * All of the `main` and `main.home` resolves are fetched as soon as possible.
142 * ```js
143 * var mainState = {
144 * name: 'main',
145 * resolve: mainResolves, // defined elsewhere
146 * resolvePolicy: { when: 'EAGER' },
147 * }
148 *
149 * var homeState = {
150 * name: 'main.home',
151 * resolve: homeResolves, // defined elsewhere
152 * resolvePolicy: { when: 'EAGER' },
153 * }
154 * ```
155 */
156 when?: PolicyWhen;
157 /**
158 * Determines the unwrapping behavior of asynchronous resolve values.
159 *
160 * - `WAIT` (default)
161 * - If a promise is returned from the resolveFn, wait for the promise before proceeding
162 * - The unwrapped value from the promise
163 * - `NOWAIT`
164 * - If a promise is returned from the resolve, do not wait for the promise.
165 * - Any other value returned is wrapped in a promise.
166 * - The promise will not be unwrapped.
167 * - The promise itself will be provided when the resolve is injected or bound elsewhere.
168 * - {@link CustomAsyncPolicy}
169 * - You can define a custom function that will be called with the resolveFn value.
170 * - This function must return a promise.
171 * - The transition will wait for this promise before proceeding
172 *
173 * NOTE: The previous `RXWAIT` policy has become a CustomAsyncPolicy function exported in `@uirouter/rx` package.
174 *
175 * #### Example:
176 * The `Transition` will not wait for the resolve promise(s) from `main` to settle before continuing.
177 * Resolves for `main` will be provided to components wrapped in a `Promise`.
178 *
179 * The `Transition` will wait for the `main.home` resolve promises.
180 * Resolved values will be unwrapped before being provided to components.
181 * ```js
182 * var mainState = {
183 * name: 'main',
184 * resolve: mainResolves, // defined elsewhere
185 * resolvePolicy: { async: 'NOWAIT' },
186 * }
187 * var homeState = {
188 * name: 'main.home',
189 * resolve: homeResolves, // defined elsewhere
190 * resolvePolicy: { async: 'WAIT' }, // default
191 * }
192 * ```
193 */
194 async?: PolicyAsync;
195}
196export declare type PolicyWhen = 'LAZY' | 'EAGER';
197export declare type PolicyAsync = 'WAIT' | 'NOWAIT' | CustomAsyncPolicy;
198export interface CustomAsyncPolicy {
199 (data: any): Promise<any>;
200}
201export declare let resolvePolicies: {
202 when: {
203 LAZY: string;
204 EAGER: string;
205 };
206 async: {
207 WAIT: string;
208 NOWAIT: string;
209 };
210};