UNPKG

2.48 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` expects 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 * Control access to an Observable
43 *
44 * ```ts
45 * import { iif, of, EMPTY } from 'rxjs';
46 *
47 * let accessGranted;
48 * const observableIfYouHaveAccess = iif(
49 * () => accessGranted,
50 * of('It seems you have an access...'),
51 * EMPTY
52 * );
53 *
54 * accessGranted = true;
55 * observableIfYouHaveAccess.subscribe({
56 * next: value => console.log(value),
57 * complete: () => console.log('The end')
58 * });
59 *
60 * // Logs:
61 * // 'It seems you have an access...'
62 * // 'The end'
63 *
64 * accessGranted = false;
65 * observableIfYouHaveAccess.subscribe({
66 * next: value => console.log(value),
67 * complete: () => 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