UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

106 lines 4.04 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 innerSubscribe_1 = require("../innerSubscribe"); /** * Converts a higher-order Observable into a first-order Observable by dropping * inner Observables while the previous inner Observable has not yet completed. * * <span class="informal">Flattens an Observable-of-Observables by dropping the * next inner Observables while the current inner is still executing.</span> * * ![](exhaust.png) * * `exhaust` subscribes to an Observable that emits Observables, also known as a * higher-order Observable. Each time it observes one of these emitted inner * Observables, the output Observable begins emitting the items emitted by that * inner Observable. So far, it behaves like {@link mergeAll}. However, * `exhaust` ignores every new inner Observable if the previous Observable has * not yet completed. Once that one completes, it will accept and flatten the * next inner Observable and repeat this process. * * ## Example * Run a finite timer for each click, only if there is no currently active timer * ```ts * import { fromEvent, interval } from 'rxjs'; * import { exhaust, map, take } from 'rxjs/operators'; * * const clicks = fromEvent(document, 'click'); * const higherOrder = clicks.pipe( * map((ev) => interval(1000).pipe(take(5))), * ); * const result = higherOrder.pipe(exhaust()); * result.subscribe(x => console.log(x)); * ``` * * @see {@link combineAll} * @see {@link concatAll} * @see {@link switchAll} * @see {@link switchMap} * @see {@link mergeAll} * @see {@link exhaustMap} * @see {@link zipAll} * * @return {Observable} An Observable that takes a source of Observables and propagates the first observable * exclusively until it completes before subscribing to the next. * @method exhaust * @owner Observable */ function exhaust() { return function (source) { return source.lift(new SwitchFirstOperator()); }; } exports.exhaust = exhaust; var SwitchFirstOperator = /** @class */ (function () { function SwitchFirstOperator() { } SwitchFirstOperator.prototype.call = function (subscriber, source) { return source.subscribe(new SwitchFirstSubscriber(subscriber)); }; return SwitchFirstOperator; }()); /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var SwitchFirstSubscriber = /** @class */ (function (_super) { __extends(SwitchFirstSubscriber, _super); function SwitchFirstSubscriber(destination) { var _this = _super.call(this, destination) || this; _this.hasCompleted = false; _this.hasSubscription = false; return _this; } SwitchFirstSubscriber.prototype._next = function (value) { if (!this.hasSubscription) { this.hasSubscription = true; this.add(innerSubscribe_1.innerSubscribe(value, new innerSubscribe_1.SimpleInnerSubscriber(this))); } }; SwitchFirstSubscriber.prototype._complete = function () { this.hasCompleted = true; if (!this.hasSubscription) { this.destination.complete(); } }; SwitchFirstSubscriber.prototype.notifyComplete = function () { this.hasSubscription = false; if (this.hasCompleted) { this.destination.complete(); } }; return SwitchFirstSubscriber; }(innerSubscribe_1.SimpleOuterSubscriber)); //# sourceMappingURL=exhaust.js.map