Front end of the Slack clone application.

timeout.js 6.6KB

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