Front end of the Slack clone application.

timeoutWith.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /** PURE_IMPORTS_START .._scheduler_async,.._util_isDate,.._OuterSubscriber,.._util_subscribeToResult 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 { OuterSubscriber } from '../OuterSubscriber';
  12. import { subscribeToResult } from '../util/subscribeToResult';
  13. /* tslint:enable:max-line-length */
  14. /**
  15. *
  16. * Errors if Observable does not emit a value in given time span, in case of which
  17. * subscribes to the second Observable.
  18. *
  19. * <span class="informal">It's a version of `timeout` operator that let's you specify fallback Observable.</span>
  20. *
  21. * <img src="./img/timeoutWith.png" width="100%">
  22. *
  23. * `timeoutWith` is a variation of `timeout` operator. It behaves exactly the same,
  24. * still accepting as a first argument either a number or a Date, which control - respectively -
  25. * when values of source Observable should be emitted or when it should complete.
  26. *
  27. * The only difference is that it accepts a second, required parameter. This parameter
  28. * should be an Observable which will be subscribed when source Observable fails any timeout check.
  29. * So whenever regular `timeout` would emit an error, `timeoutWith` will instead start re-emitting
  30. * values from second Observable. Note that this fallback Observable is not checked for timeouts
  31. * itself, so it can emit values and complete at arbitrary points in time. From the moment of a second
  32. * subscription, Observable returned from `timeoutWith` simply mirrors fallback stream. When that
  33. * stream completes, it completes as well.
  34. *
  35. * Scheduler, which in case of `timeout` is provided as as second argument, can be still provided
  36. * here - as a third, optional parameter. It still is used to schedule timeout checks and -
  37. * as a consequence - when second Observable will be subscribed, since subscription happens
  38. * immediately after failing check.
  39. *
  40. * @example <caption>Add fallback observable</caption>
  41. * const seconds = Rx.Observable.interval(1000);
  42. * const minutes = Rx.Observable.interval(60 * 1000);
  43. *
  44. * seconds.timeoutWith(900, minutes)
  45. * .subscribe(
  46. * value => console.log(value), // After 900ms, will start emitting `minutes`,
  47. * // since first value of `seconds` will not arrive fast enough.
  48. * err => console.log(err) // Would be called after 900ms in case of `timeout`,
  49. * // but here will never be called.
  50. * );
  51. *
  52. * @param {number|Date} due Number specifying period within which Observable must emit values
  53. * or Date specifying before when Observable should complete
  54. * @param {Observable<T>} withObservable Observable which will be subscribed if source fails timeout check.
  55. * @param {Scheduler} [scheduler] Scheduler controlling when timeout checks occur.
  56. * @return {Observable<T>} Observable that mirrors behaviour of source or, when timeout check fails, of an Observable
  57. * passed as a second parameter.
  58. * @method timeoutWith
  59. * @owner Observable
  60. */
  61. export function timeoutWith(due, withObservable, scheduler) {
  62. if (scheduler === void 0) {
  63. scheduler = async;
  64. }
  65. return function (source) {
  66. var absoluteTimeout = isDate(due);
  67. var waitFor = absoluteTimeout ? (+due - scheduler.now()) : Math.abs(due);
  68. return source.lift(new TimeoutWithOperator(waitFor, absoluteTimeout, withObservable, scheduler));
  69. };
  70. }
  71. var TimeoutWithOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  72. function TimeoutWithOperator(waitFor, absoluteTimeout, withObservable, scheduler) {
  73. this.waitFor = waitFor;
  74. this.absoluteTimeout = absoluteTimeout;
  75. this.withObservable = withObservable;
  76. this.scheduler = scheduler;
  77. }
  78. TimeoutWithOperator.prototype.call = function (subscriber, source) {
  79. return source.subscribe(new TimeoutWithSubscriber(subscriber, this.absoluteTimeout, this.waitFor, this.withObservable, this.scheduler));
  80. };
  81. return TimeoutWithOperator;
  82. }());
  83. /**
  84. * We need this JSDoc comment for affecting ESDoc.
  85. * @ignore
  86. * @extends {Ignored}
  87. */
  88. var TimeoutWithSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  89. __extends(TimeoutWithSubscriber, _super);
  90. function TimeoutWithSubscriber(destination, absoluteTimeout, waitFor, withObservable, scheduler) {
  91. _super.call(this, destination);
  92. this.absoluteTimeout = absoluteTimeout;
  93. this.waitFor = waitFor;
  94. this.withObservable = withObservable;
  95. this.scheduler = scheduler;
  96. this.action = null;
  97. this.scheduleTimeout();
  98. }
  99. TimeoutWithSubscriber.dispatchTimeout = function (subscriber) {
  100. var withObservable = subscriber.withObservable;
  101. subscriber._unsubscribeAndRecycle();
  102. subscriber.add(subscribeToResult(subscriber, withObservable));
  103. };
  104. TimeoutWithSubscriber.prototype.scheduleTimeout = function () {
  105. var action = this.action;
  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(TimeoutWithSubscriber.dispatchTimeout, this.waitFor, this));
  116. }
  117. };
  118. TimeoutWithSubscriber.prototype._next = function (value) {
  119. if (!this.absoluteTimeout) {
  120. this.scheduleTimeout();
  121. }
  122. _super.prototype._next.call(this, value);
  123. };
  124. /** @deprecated internal use only */ TimeoutWithSubscriber.prototype._unsubscribe = function () {
  125. this.action = null;
  126. this.scheduler = null;
  127. this.withObservable = null;
  128. };
  129. return TimeoutWithSubscriber;
  130. }(OuterSubscriber));
  131. //# sourceMappingURL=timeoutWith.js.map