Skip to content

Commit ab1aaa4

Browse files
committed
feat: add rxwait custom async policy
1 parent 05d4ad4 commit ab1aaa4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './ui-router-rx';
2+
export * from './rx-async-policy';

src/rx-async-policy.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
};

0 commit comments

Comments
 (0)