44e6a370d871a90d9071a7169441e7030934900d72ac831e3eba6165e7a4a90b6f1843e508240ae66bc6c26b361435226cdb8d78de394979f2a66b72cc1473 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { async } from '../scheduler/async';
  2. import { isValidDate } from '../util/isDate';
  3. import { ObservableInput, OperatorFunction, SchedulerLike } from '../types';
  4. import { timeout } from './timeout';
  5. /** @deprecated Replaced with {@link timeout}. Instead of `timeoutWith(someDate, a$, scheduler)`, use the configuration object
  6. * `timeout({ first: someDate, with: () => a$, scheduler })`. Will be removed in v8. */
  7. export function timeoutWith<T, R>(dueBy: Date, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R>;
  8. /** @deprecated Replaced with {@link timeout}. Instead of `timeoutWith(100, a$, scheduler)`, use the configuration object
  9. * `timeout({ each: 100, with: () => a$, scheduler })`. Will be removed in v8. */
  10. export function timeoutWith<T, R>(waitFor: number, switchTo: ObservableInput<R>, scheduler?: SchedulerLike): OperatorFunction<T, T | R>;
  11. /**
  12. * When the passed timespan elapses before the source emits any given value, it will unsubscribe from the source,
  13. * and switch the subscription to another observable.
  14. *
  15. * <span class="informal">Used to switch to a different observable if your source is being slow.</span>
  16. *
  17. * Useful in cases where:
  18. *
  19. * - You want to switch to a different source that may be faster.
  20. * - You want to notify a user that the data stream is slow.
  21. * - You want to emit a custom error rather than the {@link TimeoutError} emitted
  22. * by the default usage of {@link timeout}.
  23. *
  24. * If the first parameter is passed as Date and the time of the Date arrives before the first value arrives from the source,
  25. * it will unsubscribe from the source and switch the subscription to another observable.
  26. *
  27. * <span class="informal">Use Date object to switch to a different observable if the first value doesn't arrive by a specific time.</span>
  28. *
  29. * Can be used to set a timeout only for the first value, however it's recommended to use the {@link timeout} operator with
  30. * the `first` configuration to get the same effect.
  31. *
  32. * ## Examples
  33. *
  34. * Fallback to a faster observable
  35. *
  36. * ```ts
  37. * import { interval, timeoutWith } from 'rxjs';
  38. *
  39. * const slow$ = interval(1000);
  40. * const faster$ = interval(500);
  41. *
  42. * slow$
  43. * .pipe(timeoutWith(900, faster$))
  44. * .subscribe(console.log);
  45. * ```
  46. *
  47. * Emit your own custom timeout error
  48. *
  49. * ```ts
  50. * import { interval, timeoutWith, throwError } from 'rxjs';
  51. *
  52. * class CustomTimeoutError extends Error {
  53. * constructor() {
  54. * super('It was too slow');
  55. * this.name = 'CustomTimeoutError';
  56. * }
  57. * }
  58. *
  59. * const slow$ = interval(1000);
  60. *
  61. * slow$
  62. * .pipe(timeoutWith(900, throwError(() => new CustomTimeoutError())))
  63. * .subscribe({
  64. * error: err => console.error(err.message)
  65. * });
  66. * ```
  67. *
  68. * @see {@link timeout}
  69. *
  70. * @param due When passed a number, used as the time (in milliseconds) allowed between each value from the source before timeout
  71. * is triggered. When passed a Date, used as the exact time at which the timeout will be triggered if the first value does not arrive.
  72. * @param withObservable The observable to switch to when timeout occurs.
  73. * @param scheduler The scheduler to use with time-related operations within this operator. Defaults to {@link asyncScheduler}
  74. * @return A function that returns an Observable that mirrors behaviour of the
  75. * source Observable, unless timeout happens when it starts emitting values
  76. * from the `ObservableInput` passed as a second parameter.
  77. * @deprecated Replaced with {@link timeout}. Instead of `timeoutWith(100, a$, scheduler)`, use {@link timeout} with the configuration
  78. * object: `timeout({ each: 100, with: () => a$, scheduler })`. Instead of `timeoutWith(someDate, a$, scheduler)`, use {@link timeout}
  79. * with the configuration object: `timeout({ first: someDate, with: () => a$, scheduler })`. Will be removed in v8.
  80. */
  81. export function timeoutWith<T, R>(
  82. due: number | Date,
  83. withObservable: ObservableInput<R>,
  84. scheduler?: SchedulerLike
  85. ): OperatorFunction<T, T | R> {
  86. let first: number | Date | undefined;
  87. let each: number | undefined;
  88. let _with: () => ObservableInput<R>;
  89. scheduler = scheduler ?? async;
  90. if (isValidDate(due)) {
  91. first = due;
  92. } else if (typeof due === 'number') {
  93. each = due;
  94. }
  95. if (withObservable) {
  96. _with = () => withObservable;
  97. } else {
  98. throw new TypeError('No observable provided to switch to');
  99. }
  100. if (first == null && each == null) {
  101. // Ensure timeout was provided at runtime.
  102. throw new TypeError('No timeout provided.');
  103. }
  104. return timeout<T, ObservableInput<R>>({
  105. first,
  106. each,
  107. scheduler,
  108. with: _with,
  109. });
  110. }