timeout.d.ts 3.4 KB

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