iif.d.ts 3.1 KB

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