timeout.js 3.6 KB

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