timeout.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var async_1 = require('../scheduler/async');
  8. var isDate_1 = require('../util/isDate');
  9. var Subscriber_1 = require('../Subscriber');
  10. var TimeoutError_1 = require('../util/TimeoutError');
  11. /**
  12. *
  13. * Errors if Observable does not emit a value in given time span.
  14. *
  15. * <span class="informal">Timeouts on Observable that doesn't emit values fast enough.</span>
  16. *
  17. * <img src="./img/timeout.png" width="100%">
  18. *
  19. * `timeout` operator accepts as an argument either a number or a Date.
  20. *
  21. * If number was provided, it returns an Observable that behaves like a source
  22. * Observable, unless there is a period of time where there is no value emitted.
  23. * So if you provide `100` as argument and first value comes after 50ms from
  24. * the moment of subscription, this value will be simply re-emitted by the resulting
  25. * Observable. If however after that 100ms passes without a second value being emitted,
  26. * stream will end with an error and source Observable will be unsubscribed.
  27. * These checks are performed throughout whole lifecycle of Observable - from the moment
  28. * it was subscribed to, until it completes or errors itself. Thus every value must be
  29. * emitted within specified period since previous value.
  30. *
  31. * If provided argument was Date, returned Observable behaves differently. It throws
  32. * if Observable did not complete before provided Date. This means that periods between
  33. * emission of particular values do not matter in this case. If Observable did not complete
  34. * before provided Date, source Observable will be unsubscribed. Other than that, resulting
  35. * stream behaves just as source Observable.
  36. *
  37. * `timeout` accepts also a Scheduler as a second parameter. It is used to schedule moment (or moments)
  38. * when returned Observable will check if source stream emitted value or completed.
  39. *
  40. * @example <caption>Check if ticks are emitted within certain timespan</caption>
  41. * const seconds = Rx.Observable.interval(1000);
  42. *
  43. * seconds.timeout(1100) // Let's use bigger timespan to be safe,
  44. * // since `interval` might fire a bit later then scheduled.
  45. * .subscribe(
  46. * value => console.log(value), // Will emit numbers just as regular `interval` would.
  47. * err => console.log(err) // Will never be called.
  48. * );
  49. *
  50. * seconds.timeout(900).subscribe(
  51. * value => console.log(value), // Will never be called.
  52. * err => console.log(err) // Will emit error before even first value is emitted,
  53. * // since it did not arrive within 900ms period.
  54. * );
  55. *
  56. * @example <caption>Use Date to check if Observable completed</caption>
  57. * const seconds = Rx.Observable.interval(1000);
  58. *
  59. * seconds.timeout(new Date("December 17, 2020 03:24:00"))
  60. * .subscribe(
  61. * value => console.log(value), // Will emit values as regular `interval` would
  62. * // until December 17, 2020 at 03:24:00.
  63. * err => console.log(err) // On December 17, 2020 at 03:24:00 it will emit an error,
  64. * // since Observable did not complete by then.
  65. * );
  66. *
  67. * @see {@link timeoutWith}
  68. *
  69. * @param {number|Date} due Number specifying period within which Observable must emit values
  70. * or Date specifying before when Observable should complete
  71. * @param {Scheduler} [scheduler] Scheduler controlling when timeout checks occur.
  72. * @return {Observable<T>} Observable that mirrors behaviour of source, unless timeout checks fail.
  73. * @method timeout
  74. * @owner Observable
  75. */
  76. function timeout(due, scheduler) {
  77. if (scheduler === void 0) { scheduler = async_1.async; }
  78. var absoluteTimeout = isDate_1.isDate(due);
  79. var waitFor = absoluteTimeout ? (+due - scheduler.now()) : Math.abs(due);
  80. return function (source) { return source.lift(new TimeoutOperator(waitFor, absoluteTimeout, scheduler, new TimeoutError_1.TimeoutError())); };
  81. }
  82. exports.timeout = timeout;
  83. var TimeoutOperator = (function () {
  84. function TimeoutOperator(waitFor, absoluteTimeout, scheduler, errorInstance) {
  85. this.waitFor = waitFor;
  86. this.absoluteTimeout = absoluteTimeout;
  87. this.scheduler = scheduler;
  88. this.errorInstance = errorInstance;
  89. }
  90. TimeoutOperator.prototype.call = function (subscriber, source) {
  91. return source.subscribe(new TimeoutSubscriber(subscriber, this.absoluteTimeout, this.waitFor, this.scheduler, this.errorInstance));
  92. };
  93. return TimeoutOperator;
  94. }());
  95. /**
  96. * We need this JSDoc comment for affecting ESDoc.
  97. * @ignore
  98. * @extends {Ignored}
  99. */
  100. var TimeoutSubscriber = (function (_super) {
  101. __extends(TimeoutSubscriber, _super);
  102. function TimeoutSubscriber(destination, absoluteTimeout, waitFor, scheduler, errorInstance) {
  103. _super.call(this, destination);
  104. this.absoluteTimeout = absoluteTimeout;
  105. this.waitFor = waitFor;
  106. this.scheduler = scheduler;
  107. this.errorInstance = errorInstance;
  108. this.action = null;
  109. this.scheduleTimeout();
  110. }
  111. TimeoutSubscriber.dispatchTimeout = function (subscriber) {
  112. subscriber.error(subscriber.errorInstance);
  113. };
  114. TimeoutSubscriber.prototype.scheduleTimeout = function () {
  115. var action = this.action;
  116. if (action) {
  117. // Recycle the action if we've already scheduled one. All the production
  118. // Scheduler Actions mutate their state/delay time and return themeselves.
  119. // VirtualActions are immutable, so they create and return a clone. In this
  120. // case, we need to set the action reference to the most recent VirtualAction,
  121. // to ensure that's the one we clone from next time.
  122. this.action = action.schedule(this, this.waitFor);
  123. }
  124. else {
  125. this.add(this.action = this.scheduler.schedule(TimeoutSubscriber.dispatchTimeout, this.waitFor, this));
  126. }
  127. };
  128. TimeoutSubscriber.prototype._next = function (value) {
  129. if (!this.absoluteTimeout) {
  130. this.scheduleTimeout();
  131. }
  132. _super.prototype._next.call(this, value);
  133. };
  134. /** @deprecated internal use only */ TimeoutSubscriber.prototype._unsubscribe = function () {
  135. this.action = null;
  136. this.scheduler = null;
  137. this.errorInstance = null;
  138. };
  139. return TimeoutSubscriber;
  140. }(Subscriber_1.Subscriber));
  141. //# sourceMappingURL=timeout.js.map