timeout.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { async } from '../scheduler/async';
  2. import { isDate } from '../util/isDate';
  3. import { Subscriber } from '../Subscriber';
  4. import { TimeoutError } from '../util/TimeoutError';
  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. export function timeout(due, scheduler = async) {
  71. const absoluteTimeout = isDate(due);
  72. const waitFor = absoluteTimeout ? (+due - scheduler.now()) : Math.abs(due);
  73. return (source) => source.lift(new TimeoutOperator(waitFor, absoluteTimeout, scheduler, new TimeoutError()));
  74. }
  75. class TimeoutOperator {
  76. constructor(waitFor, absoluteTimeout, scheduler, errorInstance) {
  77. this.waitFor = waitFor;
  78. this.absoluteTimeout = absoluteTimeout;
  79. this.scheduler = scheduler;
  80. this.errorInstance = errorInstance;
  81. }
  82. call(subscriber, source) {
  83. return source.subscribe(new TimeoutSubscriber(subscriber, this.absoluteTimeout, this.waitFor, this.scheduler, this.errorInstance));
  84. }
  85. }
  86. /**
  87. * We need this JSDoc comment for affecting ESDoc.
  88. * @ignore
  89. * @extends {Ignored}
  90. */
  91. class TimeoutSubscriber extends Subscriber {
  92. constructor(destination, absoluteTimeout, waitFor, scheduler, errorInstance) {
  93. super(destination);
  94. this.absoluteTimeout = absoluteTimeout;
  95. this.waitFor = waitFor;
  96. this.scheduler = scheduler;
  97. this.errorInstance = errorInstance;
  98. this.action = null;
  99. this.scheduleTimeout();
  100. }
  101. static dispatchTimeout(subscriber) {
  102. subscriber.error(subscriber.errorInstance);
  103. }
  104. scheduleTimeout() {
  105. const { action } = this;
  106. if (action) {
  107. // Recycle the action if we've already scheduled one. All the production
  108. // Scheduler Actions mutate their state/delay time and return themeselves.
  109. // VirtualActions are immutable, so they create and return a clone. In this
  110. // case, we need to set the action reference to the most recent VirtualAction,
  111. // to ensure that's the one we clone from next time.
  112. this.action = action.schedule(this, this.waitFor);
  113. }
  114. else {
  115. this.add(this.action = this.scheduler.schedule(TimeoutSubscriber.dispatchTimeout, this.waitFor, this));
  116. }
  117. }
  118. _next(value) {
  119. if (!this.absoluteTimeout) {
  120. this.scheduleTimeout();
  121. }
  122. super._next(value);
  123. }
  124. /** @deprecated internal use only */ _unsubscribe() {
  125. this.action = null;
  126. this.scheduler = null;
  127. this.errorInstance = null;
  128. }
  129. }
  130. //# sourceMappingURL=timeout.js.map