UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

133 lines 5.27 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Subject_1 = require("../Subject"); var innerSubscribe_1 = require("../innerSubscribe"); /** * Returns an Observable that mirrors the source Observable with the exception of a `complete`. If the source * Observable calls `complete`, this method will emit to the Observable returned from `notifier`. If that Observable * calls `complete` or `error`, then this method will call `complete` or `error` on the child subscription. Otherwise * this method will resubscribe to the source Observable. * * ![](repeatWhen.png) * * ## Example * Repeat a message stream on click * ```ts * import { of, fromEvent } from 'rxjs'; * import { repeatWhen } from 'rxjs/operators'; * * const source = of('Repeat message'); * const documentClick$ = fromEvent(document, 'click'); * * source.pipe(repeatWhen(() => documentClick$) * ).subscribe(data => console.log(data)) * ``` * @see {@link repeat} * @see {@link retry} * @see {@link retryWhen} * * @param {function(notifications: Observable): Observable} notifier - Receives an Observable of notifications with * which a user can `complete` or `error`, aborting the repetition. * @return {Observable} The source Observable modified with repeat logic. * @method repeatWhen * @owner Observable */ function repeatWhen(notifier) { return function (source) { return source.lift(new RepeatWhenOperator(notifier)); }; } exports.repeatWhen = repeatWhen; var RepeatWhenOperator = /** @class */ (function () { function RepeatWhenOperator(notifier) { this.notifier = notifier; } RepeatWhenOperator.prototype.call = function (subscriber, source) { return source.subscribe(new RepeatWhenSubscriber(subscriber, this.notifier, source)); }; return RepeatWhenOperator; }()); /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var RepeatWhenSubscriber = /** @class */ (function (_super) { __extends(RepeatWhenSubscriber, _super); function RepeatWhenSubscriber(destination, notifier, source) { var _this = _super.call(this, destination) || this; _this.notifier = notifier; _this.source = source; _this.sourceIsBeingSubscribedTo = true; return _this; } RepeatWhenSubscriber.prototype.notifyNext = function () { this.sourceIsBeingSubscribedTo = true; this.source.subscribe(this); }; RepeatWhenSubscriber.prototype.notifyComplete = function () { if (this.sourceIsBeingSubscribedTo === false) { return _super.prototype.complete.call(this); } }; RepeatWhenSubscriber.prototype.complete = function () { this.sourceIsBeingSubscribedTo = false; if (!this.isStopped) { if (!this.retries) { this.subscribeToRetries(); } if (!this.retriesSubscription || this.retriesSubscription.closed) { return _super.prototype.complete.call(this); } this._unsubscribeAndRecycle(); this.notifications.next(undefined); } }; /** @deprecated This is an internal implementation detail, do not use. */ RepeatWhenSubscriber.prototype._unsubscribe = function () { var _a = this, notifications = _a.notifications, retriesSubscription = _a.retriesSubscription; if (notifications) { notifications.unsubscribe(); this.notifications = undefined; } if (retriesSubscription) { retriesSubscription.unsubscribe(); this.retriesSubscription = undefined; } this.retries = undefined; }; /** @deprecated This is an internal implementation detail, do not use. */ RepeatWhenSubscriber.prototype._unsubscribeAndRecycle = function () { var _unsubscribe = this._unsubscribe; this._unsubscribe = null; _super.prototype._unsubscribeAndRecycle.call(this); this._unsubscribe = _unsubscribe; return this; }; RepeatWhenSubscriber.prototype.subscribeToRetries = function () { this.notifications = new Subject_1.Subject(); var retries; try { var notifier = this.notifier; retries = notifier(this.notifications); } catch (e) { return _super.prototype.complete.call(this); } this.retries = retries; this.retriesSubscription = innerSubscribe_1.innerSubscribe(retries, new innerSubscribe_1.SimpleInnerSubscriber(this)); }; return RepeatWhenSubscriber; }(innerSubscribe_1.SimpleOuterSubscriber)); //# sourceMappingURL=repeatWhen.js.map