repeatWhen.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Subject_1 = require('../Subject');
  8. var tryCatch_1 = require('../util/tryCatch');
  9. var errorObject_1 = require('../util/errorObject');
  10. var OuterSubscriber_1 = require('../OuterSubscriber');
  11. var subscribeToResult_1 = require('../util/subscribeToResult');
  12. /**
  13. * Returns an Observable that mirrors the source Observable with the exception of a `complete`. If the source
  14. * Observable calls `complete`, this method will emit to the Observable returned from `notifier`. If that Observable
  15. * calls `complete` or `error`, then this method will call `complete` or `error` on the child subscription. Otherwise
  16. * this method will resubscribe to the source Observable.
  17. *
  18. * <img src="./img/repeatWhen.png" width="100%">
  19. *
  20. * @param {function(notifications: Observable): Observable} notifier - Receives an Observable of notifications with
  21. * which a user can `complete` or `error`, aborting the repetition.
  22. * @return {Observable} The source Observable modified with repeat logic.
  23. * @method repeatWhen
  24. * @owner Observable
  25. */
  26. function repeatWhen(notifier) {
  27. return function (source) { return source.lift(new RepeatWhenOperator(notifier)); };
  28. }
  29. exports.repeatWhen = repeatWhen;
  30. var RepeatWhenOperator = (function () {
  31. function RepeatWhenOperator(notifier) {
  32. this.notifier = notifier;
  33. }
  34. RepeatWhenOperator.prototype.call = function (subscriber, source) {
  35. return source.subscribe(new RepeatWhenSubscriber(subscriber, this.notifier, source));
  36. };
  37. return RepeatWhenOperator;
  38. }());
  39. /**
  40. * We need this JSDoc comment for affecting ESDoc.
  41. * @ignore
  42. * @extends {Ignored}
  43. */
  44. var RepeatWhenSubscriber = (function (_super) {
  45. __extends(RepeatWhenSubscriber, _super);
  46. function RepeatWhenSubscriber(destination, notifier, source) {
  47. _super.call(this, destination);
  48. this.notifier = notifier;
  49. this.source = source;
  50. this.sourceIsBeingSubscribedTo = true;
  51. }
  52. RepeatWhenSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  53. this.sourceIsBeingSubscribedTo = true;
  54. this.source.subscribe(this);
  55. };
  56. RepeatWhenSubscriber.prototype.notifyComplete = function (innerSub) {
  57. if (this.sourceIsBeingSubscribedTo === false) {
  58. return _super.prototype.complete.call(this);
  59. }
  60. };
  61. RepeatWhenSubscriber.prototype.complete = function () {
  62. this.sourceIsBeingSubscribedTo = false;
  63. if (!this.isStopped) {
  64. if (!this.retries) {
  65. this.subscribeToRetries();
  66. }
  67. if (!this.retriesSubscription || this.retriesSubscription.closed) {
  68. return _super.prototype.complete.call(this);
  69. }
  70. this._unsubscribeAndRecycle();
  71. this.notifications.next();
  72. }
  73. };
  74. /** @deprecated internal use only */ RepeatWhenSubscriber.prototype._unsubscribe = function () {
  75. var _a = this, notifications = _a.notifications, retriesSubscription = _a.retriesSubscription;
  76. if (notifications) {
  77. notifications.unsubscribe();
  78. this.notifications = null;
  79. }
  80. if (retriesSubscription) {
  81. retriesSubscription.unsubscribe();
  82. this.retriesSubscription = null;
  83. }
  84. this.retries = null;
  85. };
  86. /** @deprecated internal use only */ RepeatWhenSubscriber.prototype._unsubscribeAndRecycle = function () {
  87. var _a = this, notifications = _a.notifications, retries = _a.retries, retriesSubscription = _a.retriesSubscription;
  88. this.notifications = null;
  89. this.retries = null;
  90. this.retriesSubscription = null;
  91. _super.prototype._unsubscribeAndRecycle.call(this);
  92. this.notifications = notifications;
  93. this.retries = retries;
  94. this.retriesSubscription = retriesSubscription;
  95. return this;
  96. };
  97. RepeatWhenSubscriber.prototype.subscribeToRetries = function () {
  98. this.notifications = new Subject_1.Subject();
  99. var retries = tryCatch_1.tryCatch(this.notifier)(this.notifications);
  100. if (retries === errorObject_1.errorObject) {
  101. return _super.prototype.complete.call(this);
  102. }
  103. this.retries = retries;
  104. this.retriesSubscription = subscribeToResult_1.subscribeToResult(this, retries);
  105. };
  106. return RepeatWhenSubscriber;
  107. }(OuterSubscriber_1.OuterSubscriber));
  108. //# sourceMappingURL=repeatWhen.js.map