a zip code crypto-currency system good for red ONLY

timeoutWith.js 5.3KB

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