Skip to content

Commit

Permalink
feat: add rxwait custom async policy
Browse files Browse the repository at this point in the history
  • Loading branch information
wawyed committed Oct 1, 2019
1 parent 05d4ad4 commit ab1aaa4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ui-router-rx';
export * from './rx-async-policy';
38 changes: 38 additions & 0 deletions src/rx-async-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CustomAsyncPolicy } from '@uirouter/core';
import { Observable, of } from 'rxjs';
import { first, shareReplay } from 'rxjs/operators';

/**
* Determines the unwrapping behavior of asynchronous resolve values.
*
* - When an Observable is returned from the resolveFn, wait until the Observable emits at least one item.
* If any other value will be converted to an Observable that emits such value.
* - The Observable item will not be unwrapped.
* - The Observable stream itself will be provided when the resolve is injected or bound elsewhere.
*
* #### Example:
*
* The `Transition` will wait for the `main.home` resolve observables to emit their first value.
* Promises will be unwrapped and returned as observables before being provided to components.
* ```js
* var mainState = {
* name: 'main',
* resolve: mainResolves, // defined elsewhere
* resolvePolicy: { async: RXWAIT },
* }
* ```
*/
export const RXWAIT: CustomAsyncPolicy = (resolveFnValue: Observable<any> | any): Promise<Observable<any>> => {
if (!(resolveFnValue instanceof Observable)) {
resolveFnValue = of(resolveFnValue);
}

const data$: Observable<any> = resolveFnValue.pipe(shareReplay(1));

return data$
.pipe(first())
.toPromise()
.then(() => {
return data$;
});
};

0 comments on commit ab1aaa4

Please sign in to comment.