timeout.d.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Observable, SchedulerLike } from 'rxjs';
  2. /**
  3. *
  4. * Errors if Observable does not emit a value in given time span.
  5. *
  6. * <span class="informal">Timeouts on Observable that doesn't emit values fast enough.</span>
  7. *
  8. * <img src="./img/timeout.png" width="100%">
  9. *
  10. * `timeout` operator accepts as an argument either a number or a Date.
  11. *
  12. * If number was provided, it returns an Observable that behaves like a source
  13. * Observable, unless there is a period of time where there is no value emitted.
  14. * So if you provide `100` as argument and first value comes after 50ms from
  15. * the moment of subscription, this value will be simply re-emitted by the resulting
  16. * Observable. If however after that 100ms passes without a second value being emitted,
  17. * stream will end with an error and source Observable will be unsubscribed.
  18. * These checks are performed throughout whole lifecycle of Observable - from the moment
  19. * it was subscribed to, until it completes or errors itself. Thus every value must be
  20. * emitted within specified period since previous value.
  21. *
  22. * If provided argument was Date, returned Observable behaves differently. It throws
  23. * if Observable did not complete before provided Date. This means that periods between
  24. * emission of particular values do not matter in this case. If Observable did not complete
  25. * before provided Date, source Observable will be unsubscribed. Other than that, resulting
  26. * stream behaves just as source Observable.
  27. *
  28. * `timeout` accepts also a Scheduler as a second parameter. It is used to schedule moment (or moments)
  29. * when returned Observable will check if source stream emitted value or completed.
  30. *
  31. * @example <caption>Check if ticks are emitted within certain timespan</caption>
  32. * const seconds = Rx.Observable.interval(1000);
  33. *
  34. * seconds.timeout(1100) // Let's use bigger timespan to be safe,
  35. * // since `interval` might fire a bit later then scheduled.
  36. * .subscribe(
  37. * value => console.log(value), // Will emit numbers just as regular `interval` would.
  38. * err => console.log(err) // Will never be called.
  39. * );
  40. *
  41. * seconds.timeout(900).subscribe(
  42. * value => console.log(value), // Will never be called.
  43. * err => console.log(err) // Will emit error before even first value is emitted,
  44. * // since it did not arrive within 900ms period.
  45. * );
  46. *
  47. * @example <caption>Use Date to check if Observable completed</caption>
  48. * const seconds = Rx.Observable.interval(1000);
  49. *
  50. * seconds.timeout(new Date("December 17, 2020 03:24:00"))
  51. * .subscribe(
  52. * value => console.log(value), // Will emit values as regular `interval` would
  53. * // until December 17, 2020 at 03:24:00.
  54. * err => console.log(err) // On December 17, 2020 at 03:24:00 it will emit an error,
  55. * // since Observable did not complete by then.
  56. * );
  57. *
  58. * @see {@link timeoutWith}
  59. *
  60. * @param {number|Date} due Number specifying period within which Observable must emit values
  61. * or Date specifying before when Observable should complete
  62. * @param {Scheduler} [scheduler] Scheduler controlling when timeout checks occur.
  63. * @return {Observable<T>} Observable that mirrors behaviour of source, unless timeout checks fail.
  64. * @method timeout
  65. * @owner Observable
  66. */
  67. export declare function timeout<T>(this: Observable<T>, due: number | Date, scheduler?: SchedulerLike): Observable<T>;