[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { defer } from './defer';
|
---|
| 3 | import { EMPTY } from './empty';
|
---|
| 4 | import { SubscribableOrPromise } from '../types';
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * Decides at subscription time which Observable will actually be subscribed.
|
---|
| 8 | *
|
---|
| 9 | * <span class="informal">`If` statement for Observables.</span>
|
---|
| 10 | *
|
---|
| 11 | * `iif` accepts a condition function and two Observables. When
|
---|
| 12 | * an Observable returned by the operator is subscribed, condition function will be called.
|
---|
| 13 | * Based on what boolean it returns at that moment, consumer will subscribe either to
|
---|
| 14 | * the first Observable (if condition was true) or to the second (if condition was false). Condition
|
---|
| 15 | * function may also not return anything - in that case condition will be evaluated as false and
|
---|
| 16 | * second Observable will be subscribed.
|
---|
| 17 | *
|
---|
| 18 | * Note that Observables for both cases (true and false) are optional. If condition points to an Observable that
|
---|
| 19 | * was left undefined, resulting stream will simply complete immediately. That allows you to, rather
|
---|
| 20 | * than controlling which Observable will be subscribed, decide at runtime if consumer should have access
|
---|
| 21 | * to given Observable or not.
|
---|
| 22 | *
|
---|
| 23 | * If you have more complex logic that requires decision between more than two Observables, {@link defer}
|
---|
| 24 | * will probably be a better choice. Actually `iif` can be easily implemented with {@link defer}
|
---|
| 25 | * and exists only for convenience and readability reasons.
|
---|
| 26 | *
|
---|
| 27 | *
|
---|
| 28 | * ## Examples
|
---|
| 29 | * ### Change at runtime which Observable will be subscribed
|
---|
| 30 | * ```ts
|
---|
| 31 | * import { iif, of } from 'rxjs';
|
---|
| 32 | *
|
---|
| 33 | * let subscribeToFirst;
|
---|
| 34 | * const firstOrSecond = iif(
|
---|
| 35 | * () => subscribeToFirst,
|
---|
| 36 | * of('first'),
|
---|
| 37 | * of('second'),
|
---|
| 38 | * );
|
---|
| 39 | *
|
---|
| 40 | * subscribeToFirst = true;
|
---|
| 41 | * firstOrSecond.subscribe(value => console.log(value));
|
---|
| 42 | *
|
---|
| 43 | * // Logs:
|
---|
| 44 | * // "first"
|
---|
| 45 | *
|
---|
| 46 | * subscribeToFirst = false;
|
---|
| 47 | * firstOrSecond.subscribe(value => console.log(value));
|
---|
| 48 | *
|
---|
| 49 | * // Logs:
|
---|
| 50 | * // "second"
|
---|
| 51 | *
|
---|
| 52 | * ```
|
---|
| 53 | *
|
---|
| 54 | * ### Control an access to an Observable
|
---|
| 55 | * ```ts
|
---|
| 56 | * let accessGranted;
|
---|
| 57 | * const observableIfYouHaveAccess = iif(
|
---|
| 58 | * () => accessGranted,
|
---|
| 59 | * of('It seems you have an access...'), // Note that only one Observable is passed to the operator.
|
---|
| 60 | * );
|
---|
| 61 | *
|
---|
| 62 | * accessGranted = true;
|
---|
| 63 | * observableIfYouHaveAccess.subscribe(
|
---|
| 64 | * value => console.log(value),
|
---|
| 65 | * err => {},
|
---|
| 66 | * () => console.log('The end'),
|
---|
| 67 | * );
|
---|
| 68 | *
|
---|
| 69 | * // Logs:
|
---|
| 70 | * // "It seems you have an access..."
|
---|
| 71 | * // "The end"
|
---|
| 72 | *
|
---|
| 73 | * accessGranted = false;
|
---|
| 74 | * observableIfYouHaveAccess.subscribe(
|
---|
| 75 | * value => console.log(value),
|
---|
| 76 | * err => {},
|
---|
| 77 | * () => console.log('The end'),
|
---|
| 78 | * );
|
---|
| 79 | *
|
---|
| 80 | * // Logs:
|
---|
| 81 | * // "The end"
|
---|
| 82 | * ```
|
---|
| 83 | *
|
---|
| 84 | * @see {@link defer}
|
---|
| 85 | *
|
---|
| 86 | * @param {function(): boolean} condition Condition which Observable should be chosen.
|
---|
| 87 | * @param {Observable} [trueObservable] An Observable that will be subscribed if condition is true.
|
---|
| 88 | * @param {Observable} [falseObservable] An Observable that will be subscribed if condition is false.
|
---|
| 89 | * @return {Observable} Either first or second Observable, depending on condition.
|
---|
| 90 | * @static true
|
---|
| 91 | * @name iif
|
---|
| 92 | * @owner Observable
|
---|
| 93 | */
|
---|
| 94 | export function iif<T = never, F = never>(
|
---|
| 95 | condition: () => boolean,
|
---|
| 96 | trueResult: SubscribableOrPromise<T> = EMPTY,
|
---|
| 97 | falseResult: SubscribableOrPromise<F> = EMPTY
|
---|
| 98 | ): Observable<T|F> {
|
---|
| 99 | return defer(() => condition() ? trueResult : falseResult);
|
---|
| 100 | }
|
---|