1c3fe48bbac739fbb5e3ea64b5eb33aa2235abbe07f8625678cb69977ad526317d7b8a399f28307f04aefb024d6b4ab1823c7d7472ae85ce225a96e80996c2 2.5 KB

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