ad359bd77f66d9d3089b3429636fb41cd66106fe7c192459283f491c95b787fd53e51e5395f8e4b1d825f78aaff627aad44a4281f9e3c4b68991b04c07b3f1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { MonoTypeOperatorFunction, SchedulerLike, OperatorFunction, ObservableInput, ObservedValueOf } from '../types';
  2. export interface TimeoutConfig<T, O extends ObservableInput<unknown> = ObservableInput<T>, M = unknown> {
  3. /**
  4. * The time allowed between values from the source before timeout is triggered.
  5. */
  6. each?: number;
  7. /**
  8. * The relative time as a `number` in milliseconds, or a specific time as a `Date` object,
  9. * by which the first value must arrive from the source before timeout is triggered.
  10. */
  11. first?: number | Date;
  12. /**
  13. * The scheduler to use with time-related operations within this operator. Defaults to {@link asyncScheduler}
  14. */
  15. scheduler?: SchedulerLike;
  16. /**
  17. * A factory used to create observable to switch to when timeout occurs. Provides
  18. * a {@link TimeoutInfo} about the source observable's emissions and what delay or
  19. * exact time triggered the timeout.
  20. */
  21. with?: (info: TimeoutInfo<T, M>) => O;
  22. /**
  23. * Optional additional metadata you can provide to code that handles
  24. * the timeout, will be provided through the {@link TimeoutError}.
  25. * This can be used to help identify the source of a timeout or pass along
  26. * other information related to the timeout.
  27. */
  28. meta?: M;
  29. }
  30. export interface TimeoutInfo<T, M = unknown> {
  31. /** Optional metadata that was provided to the timeout configuration. */
  32. readonly meta: M;
  33. /** The number of messages seen before the timeout */
  34. readonly seen: number;
  35. /** The last message seen */
  36. readonly lastValue: T | null;
  37. }
  38. /**
  39. * An error emitted when a timeout occurs.
  40. */
  41. export interface TimeoutError<T = unknown, M = unknown> extends Error {
  42. /**
  43. * The information provided to the error by the timeout
  44. * operation that created the error. Will be `null` if
  45. * used directly in non-RxJS code with an empty constructor.
  46. * (Note that using this constructor directly is not recommended,
  47. * you should create your own errors)
  48. */
  49. info: TimeoutInfo<T, M> | null;
  50. }
  51. export interface TimeoutErrorCtor {
  52. /**
  53. * @deprecated Internal implementation detail. Do not construct error instances.
  54. * Cannot be tagged as internal: https://github.com/ReactiveX/rxjs/issues/6269
  55. */
  56. new <T = unknown, M = unknown>(info?: TimeoutInfo<T, M>): TimeoutError<T, M>;
  57. }
  58. /**
  59. * An error thrown by the {@link timeout} operator.
  60. *
  61. * Provided so users can use as a type and do quality comparisons.
  62. * We recommend you do not subclass this or create instances of this class directly.
  63. * If you have need of a error representing a timeout, you should
  64. * create your own error class and use that.
  65. *
  66. * @see {@link timeout}
  67. */
  68. export declare const TimeoutError: TimeoutErrorCtor;
  69. /**
  70. * If `with` is provided, this will return an observable that will switch to a different observable if the source
  71. * does not push values within the specified time parameters.
  72. *
  73. * <span class="informal">The most flexible option for creating a timeout behavior.</span>
  74. *
  75. * The first thing to know about the configuration is if you do not provide a `with` property to the configuration,
  76. * when timeout conditions are met, this operator will emit a {@link TimeoutError}. Otherwise, it will use the factory
  77. * function provided by `with`, and switch your subscription to the result of that. Timeout conditions are provided by
  78. * the settings in `first` and `each`.
  79. *
  80. * The `first` property can be either a `Date` for a specific time, a `number` for a time period relative to the
  81. * point of subscription, or it can be skipped. This property is to check timeout conditions for the arrival of
  82. * the first value from the source _only_. The timings of all subsequent values from the source will be checked
  83. * against the time period provided by `each`, if it was provided.
  84. *
  85. * The `each` property can be either a `number` or skipped. If a value for `each` is provided, it represents the amount of
  86. * time the resulting observable will wait between the arrival of values from the source before timing out. Note that if
  87. * `first` is _not_ provided, the value from `each` will be used to check timeout conditions for the arrival of the first
  88. * value and all subsequent values. If `first` _is_ provided, `each` will only be use to check all values after the first.
  89. *
  90. * ## Examples
  91. *
  92. * Emit a custom error if there is too much time between values
  93. *
  94. * ```ts
  95. * import { interval, timeout, throwError } from 'rxjs';
  96. *
  97. * class CustomTimeoutError extends Error {
  98. * constructor() {
  99. * super('It was too slow');
  100. * this.name = 'CustomTimeoutError';
  101. * }
  102. * }
  103. *
  104. * const slow$ = interval(900);
  105. *
  106. * slow$.pipe(
  107. * timeout({
  108. * each: 1000,
  109. * with: () => throwError(() => new CustomTimeoutError())
  110. * })
  111. * )
  112. * .subscribe({
  113. * error: console.error
  114. * });
  115. * ```
  116. *
  117. * Switch to a faster observable if your source is slow.
  118. *
  119. * ```ts
  120. * import { interval, timeout } from 'rxjs';
  121. *
  122. * const slow$ = interval(900);
  123. * const fast$ = interval(500);
  124. *
  125. * slow$.pipe(
  126. * timeout({
  127. * each: 1000,
  128. * with: () => fast$,
  129. * })
  130. * )
  131. * .subscribe(console.log);
  132. * ```
  133. * @param config The configuration for the timeout.
  134. */
  135. export declare function timeout<T, O extends ObservableInput<unknown>, M = unknown>(config: TimeoutConfig<T, O, M> & {
  136. with: (info: TimeoutInfo<T, M>) => O;
  137. }): OperatorFunction<T, T | ObservedValueOf<O>>;
  138. /**
  139. * Returns an observable that will error or switch to a different observable if the source does not push values
  140. * within the specified time parameters.
  141. *
  142. * <span class="informal">The most flexible option for creating a timeout behavior.</span>
  143. *
  144. * The first thing to know about the configuration is if you do not provide a `with` property to the configuration,
  145. * when timeout conditions are met, this operator will emit a {@link TimeoutError}. Otherwise, it will use the factory
  146. * function provided by `with`, and switch your subscription to the result of that. Timeout conditions are provided by
  147. * the settings in `first` and `each`.
  148. *
  149. * The `first` property can be either a `Date` for a specific time, a `number` for a time period relative to the
  150. * point of subscription, or it can be skipped. This property is to check timeout conditions for the arrival of
  151. * the first value from the source _only_. The timings of all subsequent values from the source will be checked
  152. * against the time period provided by `each`, if it was provided.
  153. *
  154. * The `each` property can be either a `number` or skipped. If a value for `each` is provided, it represents the amount of
  155. * time the resulting observable will wait between the arrival of values from the source before timing out. Note that if
  156. * `first` is _not_ provided, the value from `each` will be used to check timeout conditions for the arrival of the first
  157. * value and all subsequent values. If `first` _is_ provided, `each` will only be use to check all values after the first.
  158. *
  159. * ### Handling TimeoutErrors
  160. *
  161. * If no `with` property was provided, subscriptions to the resulting observable may emit an error of {@link TimeoutError}.
  162. * The timeout error provides useful information you can examine when you're handling the error. The most common way to handle
  163. * the error would be with {@link catchError}, although you could use {@link tap} or just the error handler in your `subscribe` call
  164. * directly, if your error handling is only a side effect (such as notifying the user, or logging).
  165. *
  166. * In this case, you would check the error for `instanceof TimeoutError` to validate that the error was indeed from `timeout`, and
  167. * not from some other source. If it's not from `timeout`, you should probably rethrow it if you're in a `catchError`.
  168. *
  169. * ## Examples
  170. *
  171. * Emit a {@link TimeoutError} if the first value, and _only_ the first value, does not arrive within 5 seconds
  172. *
  173. * ```ts
  174. * import { interval, timeout } from 'rxjs';
  175. *
  176. * // A random interval that lasts between 0 and 10 seconds per tick
  177. * const source$ = interval(Math.round(Math.random() * 10_000));
  178. *
  179. * source$.pipe(
  180. * timeout({ first: 5_000 })
  181. * )
  182. * .subscribe({
  183. * next: console.log,
  184. * error: console.error
  185. * });
  186. * ```
  187. *
  188. * Emit a {@link TimeoutError} if the source waits longer than 5 seconds between any two values or the first value
  189. * and subscription.
  190. *
  191. * ```ts
  192. * import { timer, timeout, expand } from 'rxjs';
  193. *
  194. * const getRandomTime = () => Math.round(Math.random() * 10_000);
  195. *
  196. * // An observable that waits a random amount of time between each delivered value
  197. * const source$ = timer(getRandomTime())
  198. * .pipe(expand(() => timer(getRandomTime())));
  199. *
  200. * source$
  201. * .pipe(timeout({ each: 5_000 }))
  202. * .subscribe({
  203. * next: console.log,
  204. * error: console.error
  205. * });
  206. * ```
  207. *
  208. * Emit a {@link TimeoutError} if the source does not emit before 7 seconds, _or_ if the source waits longer than
  209. * 5 seconds between any two values after the first.
  210. *
  211. * ```ts
  212. * import { timer, timeout, expand } from 'rxjs';
  213. *
  214. * const getRandomTime = () => Math.round(Math.random() * 10_000);
  215. *
  216. * // An observable that waits a random amount of time between each delivered value
  217. * const source$ = timer(getRandomTime())
  218. * .pipe(expand(() => timer(getRandomTime())));
  219. *
  220. * source$
  221. * .pipe(timeout({ first: 7_000, each: 5_000 }))
  222. * .subscribe({
  223. * next: console.log,
  224. * error: console.error
  225. * });
  226. * ```
  227. */
  228. export declare function timeout<T, M = unknown>(config: Omit<TimeoutConfig<T, any, M>, 'with'>): OperatorFunction<T, T>;
  229. /**
  230. * Returns an observable that will error if the source does not push its first value before the specified time passed as a `Date`.
  231. * This is functionally the same as `timeout({ first: someDate })`.
  232. *
  233. * <span class="informal">Errors if the first value doesn't show up before the given date and time</span>
  234. *
  235. * ![](timeout.png)
  236. *
  237. * @param first The date to at which the resulting observable will timeout if the source observable
  238. * does not emit at least one value.
  239. * @param scheduler The scheduler to use. Defaults to {@link asyncScheduler}.
  240. */
  241. export declare function timeout<T>(first: Date, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
  242. /**
  243. * Returns an observable that will error if the source does not push a value within the specified time in milliseconds.
  244. * This is functionally the same as `timeout({ each: milliseconds })`.
  245. *
  246. * <span class="informal">Errors if it waits too long between any value</span>
  247. *
  248. * ![](timeout.png)
  249. *
  250. * @param each The time allowed between each pushed value from the source before the resulting observable
  251. * will timeout.
  252. * @param scheduler The scheduler to use. Defaults to {@link asyncScheduler}.
  253. */
  254. export declare function timeout<T>(each: number, scheduler?: SchedulerLike): MonoTypeOperatorFunction<T>;
  255. //# sourceMappingURL=timeout.d.ts.map