Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

iif.ts 3.2KB

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