import { Observable } from '../Observable'; import { defer } from './defer'; import { EMPTY } from './empty'; import { SubscribableOrPromise } from '../types'; /** * Decides at subscription time which Observable will actually be subscribed. * * <span class="informal">`If` statement for Observables.</span> * * `iif` accepts a condition function and two Observables. When * an Observable returned by the operator is subscribed, condition function will be called. * Based on what boolean it returns at that moment, consumer will subscribe either to * the first Observable (if condition was true) or to the second (if condition was false). Condition * function may also not return anything - in that case condition will be evaluated as false and * second Observable will be subscribed. * * Note that Observables for both cases (true and false) are optional. If condition points to an Observable that * was left undefined, resulting stream will simply complete immediately. That allows you to, rather * than controlling which Observable will be subscribed, decide at runtime if consumer should have access * to given Observable or not. * * If you have more complex logic that requires decision between more than two Observables, {@link defer} * will probably be a better choice. Actually `iif` can be easily implemented with {@link defer} * and exists only for convenience and readability reasons. * * * ## Examples * ### Change at runtime which Observable will be subscribed * ```ts * import { iif, of } from 'rxjs'; * * let subscribeToFirst; * const firstOrSecond = iif( * () => subscribeToFirst, * of('first'), * of('second'), * ); * * subscribeToFirst = true; * firstOrSecond.subscribe(value => console.log(value)); * * // Logs: * // "first" * * subscribeToFirst = false; * firstOrSecond.subscribe(value => console.log(value)); * * // Logs: * // "second" * * ``` * * ### Control an access to an Observable * ```ts * let accessGranted; * const observableIfYouHaveAccess = iif( * () => accessGranted, * of('It seems you have an access...'), // Note that only one Observable is passed to the operator. * ); * * accessGranted = true; * observableIfYouHaveAccess.subscribe( * value => console.log(value), * err => {}, * () => console.log('The end'), * ); * * // Logs: * // "It seems you have an access..." * // "The end" * * accessGranted = false; * observableIfYouHaveAccess.subscribe( * value => console.log(value), * err => {}, * () => console.log('The end'), * ); * * // Logs: * // "The end" * ``` * * @see {@link defer} * * @param {function(): boolean} condition Condition which Observable should be chosen. * @param {Observable} [trueObservable] An Observable that will be subscribed if condition is true. * @param {Observable} [falseObservable] An Observable that will be subscribed if condition is false. * @return {Observable} Either first or second Observable, depending on condition. * @static true * @name iif * @owner Observable */ export function iif<T = never, F = never>( condition: () => boolean, trueResult: SubscribableOrPromise<T> = EMPTY, falseResult: SubscribableOrPromise<F> = EMPTY ): Observable<T|F> { return defer(() => condition() ? trueResult : falseResult); }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
dom | Folder | 0755 |
|
|
ConnectableObservable.ts | File | 5.42 KB | 0644 |
|
SubscribeOnObservable.ts | File | 1.59 KB | 0644 |
|
bindCallback.ts | File | 15.9 KB | 0644 |
|
bindNodeCallback.ts | File | 15.41 KB | 0644 |
|
combineLatest.ts | File | 21.95 KB | 0644 |
|
concat.ts | File | 9.43 KB | 0644 |
|
defer.ts | File | 2.53 KB | 0644 |
|
empty.ts | File | 2.21 KB | 0644 |
|
forkJoin.ts | File | 9.49 KB | 0644 |
|
from.ts | File | 3.06 KB | 0644 |
|
fromArray.ts | File | 423 B | 0644 |
|
fromEvent.ts | File | 10.38 KB | 0644 |
|
fromEventPattern.ts | File | 7.14 KB | 0644 |
|
fromIterable.ts | File | 511 B | 0644 |
|
fromObservable.ts | File | 485 B | 0644 |
|
fromPromise.ts | File | 439 B | 0644 |
|
generate.ts | File | 12.55 KB | 0644 |
|
iif.ts | File | 3.22 KB | 0644 |
|
interval.ts | File | 2.53 KB | 0644 |
|
merge.ts | File | 9.94 KB | 0644 |
|
never.ts | File | 1.16 KB | 0644 |
|
of.ts | File | 4.77 KB | 0644 |
|
onErrorResumeNext.ts | File | 3.83 KB | 0644 |
|
pairs.ts | File | 3.06 KB | 0644 |
|
partition.ts | File | 2.61 KB | 0644 |
|
race.ts | File | 5.03 KB | 0644 |
|
range.ts | File | 2.28 KB | 0644 |
|
throwError.ts | File | 2.2 KB | 0644 |
|
timer.ts | File | 3.32 KB | 0644 |
|
using.ts | File | 3.04 KB | 0644 |
|
zip.ts | File | 12.06 KB | 0644 |
|