timeoutWith.js 6.2KB

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