UNPKG

2.5 kBTypeScriptView Raw
1import { Observable } from '../Observable';
2import { ObservableInput } from '../types';
3/**
4 * Checks a boolean at subscription time, and chooses between one of two observable sources
5 *
6 * `iif` excepts a function that returns a boolean (the `condition` function), and two sources,
7 * the `trueResult` and the `falseResult`, and returns an Observable.
8 *
9 * At the moment of subscription, the `condition` function is called. If the result is `true`, the
10 * subscription will be to the source passed as the `trueResult`, otherwise, the subscription will be
11 * to the source passed as the `falseResult`.
12 *
13 * If you need to check more than two options to choose between more than one observable, have a look at the {@link defer} creation method.
14 *
15 * ## Examples
16 *
17 * ### Change at runtime which Observable will be subscribed
18 *
19 * ```ts
20 * import { iif, of } from 'rxjs';
21 *
22 * let subscribeToFirst;
23 * const firstOrSecond = iif(
24 * () => subscribeToFirst,
25 * of('first'),
26 * of('second'),
27 * );
28 *
29 * subscribeToFirst = true;
30 * firstOrSecond.subscribe(value => console.log(value));
31 *
32 * // Logs:
33 * // "first"
34 *
35 * subscribeToFirst = false;
36 * firstOrSecond.subscribe(value => console.log(value));
37 *
38 * // Logs:
39 * // "second"
40 *
41 * ```
42 *
43 * ### Control an access to an Observable
44 *
45 * ```ts
46 * let accessGranted;
47 * const observableIfYouHaveAccess = iif(
48 * () => accessGranted,
49 * of('It seems you have an access...'), // Note that only one Observable is passed to the operator.
50 * );
51 *
52 * accessGranted = true;
53 * observableIfYouHaveAccess.subscribe(
54 * value => console.log(value),
55 * err => {},
56 * () => console.log('The end'),
57 * );
58 *
59 * // Logs:
60 * // "It seems you have an access..."
61 * // "The end"
62 *
63 * accessGranted = false;
64 * observableIfYouHaveAccess.subscribe(
65 * value => console.log(value),
66 * err => {},
67 * () => console.log('The end'),
68 * );
69 *
70 * // Logs:
71 * // "The end"
72 * ```
73 *
74 * @see {@link defer}
75 *
76 * @param condition Condition which Observable should be chosen.
77 * @param trueResult An Observable that will be subscribed if condition is true.
78 * @param falseResult An Observable that will be subscribed if condition is false.
79 * @return An observable that proxies to `trueResult` or `falseResult`, depending on the result of the `condition` function.
80 */
81export declare function iif<T, F>(condition: () => boolean, trueResult: ObservableInput<T>, falseResult: ObservableInput<F>): Observable<T | F>;
82//# sourceMappingURL=iif.d.ts.map
\No newline at end of file