Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iif.d.ts 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * ```javascript
  28. * let subscribeToFirst;
  29. * const firstOrSecond = iif(
  30. * () => subscribeToFirst,
  31. * of('first'),
  32. * of('second'),
  33. * );
  34. *
  35. * subscribeToFirst = true;
  36. * firstOrSecond.subscribe(value => console.log(value));
  37. *
  38. * // Logs:
  39. * // "first"
  40. *
  41. * subscribeToFirst = false;
  42. * firstOrSecond.subscribe(value => console.log(value));
  43. *
  44. * // Logs:
  45. * // "second"
  46. *
  47. * ```
  48. *
  49. * ### Control an access to an Observable
  50. * ```javascript
  51. * let accessGranted;
  52. * const observableIfYouHaveAccess = iif(
  53. * () => accessGranted,
  54. * of('It seems you have an access...'), // Note that only one Observable is passed to the operator.
  55. * );
  56. *
  57. * accessGranted = true;
  58. * observableIfYouHaveAccess.subscribe(
  59. * value => console.log(value),
  60. * err => {},
  61. * () => console.log('The end'),
  62. * );
  63. *
  64. * // Logs:
  65. * // "It seems you have an access..."
  66. * // "The end"
  67. *
  68. * accessGranted = false;
  69. * observableIfYouHaveAccess.subscribe(
  70. * value => console.log(value),
  71. * err => {},
  72. * () => console.log('The end'),
  73. * );
  74. *
  75. * // Logs:
  76. * // "The end"
  77. * ```
  78. *
  79. * @see {@link defer}
  80. *
  81. * @param {function(): boolean} condition Condition which Observable should be chosen.
  82. * @param {Observable} [trueObservable] An Observable that will be subscribed if condition is true.
  83. * @param {Observable} [falseObservable] An Observable that will be subscribed if condition is false.
  84. * @return {Observable} Either first or second Observable, depending on condition.
  85. * @static true
  86. * @name iif
  87. * @owner Observable
  88. */
  89. export declare function iif<T, F>(condition: () => boolean, trueResult?: SubscribableOrPromise<T>, falseResult?: SubscribableOrPromise<F>): Observable<T | F>;