e324f9e8497015f1ac101f64f187925faf5e58d2cd02473ac493fb17e0201ffc519e510c63b1e6c0ad50404d3914bb2df1e39c2f589874aaa676345e58546c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Observable } from '../Observable';
  2. import { 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. */
  81. export declare function iif<T, F>(condition: () => boolean, trueResult: ObservableInput<T>, falseResult: ObservableInput<F>): Observable<T | F>;
  82. //# sourceMappingURL=iif.d.ts.map