File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1
1
export * from './ui-router-rx' ;
2
+ export * from './rx-async-policy' ;
Original file line number Diff line number Diff line change
1
+ import { CustomAsyncPolicy } from '@uirouter/core' ;
2
+ import { Observable , of } from 'rxjs' ;
3
+ import { first , shareReplay } from 'rxjs/operators' ;
4
+
5
+ /**
6
+ * Determines the unwrapping behavior of asynchronous resolve values.
7
+ *
8
+ * - When an Observable is returned from the resolveFn, wait until the Observable emits at least one item.
9
+ * If any other value will be converted to an Observable that emits such value.
10
+ * - The Observable item will not be unwrapped.
11
+ * - The Observable stream itself will be provided when the resolve is injected or bound elsewhere.
12
+ *
13
+ * #### Example:
14
+ *
15
+ * The `Transition` will wait for the `main.home` resolve observables to emit their first value.
16
+ * Promises will be unwrapped and returned as observables before being provided to components.
17
+ * ```js
18
+ * var mainState = {
19
+ * name: 'main',
20
+ * resolve: mainResolves, // defined elsewhere
21
+ * resolvePolicy: { async: RXWAIT },
22
+ * }
23
+ * ```
24
+ */
25
+ export const RXWAIT : CustomAsyncPolicy = ( resolveFnValue : Observable < any > | any ) : Promise < Observable < any > > => {
26
+ if ( ! ( resolveFnValue instanceof Observable ) ) {
27
+ resolveFnValue = of ( resolveFnValue ) ;
28
+ }
29
+
30
+ const data$ : Observable < any > = resolveFnValue . pipe ( shareReplay ( 1 ) ) ;
31
+
32
+ return data$
33
+ . pipe ( first ( ) )
34
+ . toPromise ( )
35
+ . then ( ( ) => {
36
+ return data$ ;
37
+ } ) ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments