UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

176 lines 6.58 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 map_1 = require("./map"); var from_1 = require("../observable/from"); var innerSubscribe_1 = require("../innerSubscribe"); /* tslint:enable:max-line-length */ /** * Projects each source value to an Observable which is merged in the output * Observable. * * <span class="informal">Maps each value to an Observable, then flattens all of * these inner Observables using {@link mergeAll}.</span> * * ![](mergeMap.png) * * Returns an Observable that emits items based on applying a function that you * supply to each item emitted by the source Observable, where that function * returns an Observable, and then merging those resulting Observables and * emitting the results of this merger. * * ## Example * Map and flatten each letter to an Observable ticking every 1 second * ```ts * import { of, interval } from 'rxjs'; * import { mergeMap, map } from 'rxjs/operators'; * * const letters = of('a', 'b', 'c'); * const result = letters.pipe( * mergeMap(x => interval(1000).pipe(map(i => x+i))), * ); * result.subscribe(x => console.log(x)); * * // Results in the following: * // a0 * // b0 * // c0 * // a1 * // b1 * // c1 * // continues to list a,b,c with respective ascending integers * ``` * * @see {@link concatMap} * @see {@link exhaustMap} * @see {@link merge} * @see {@link mergeAll} * @see {@link mergeMapTo} * @see {@link mergeScan} * @see {@link switchMap} * * @param {function(value: T, ?index: number): ObservableInput} project A function * that, when applied to an item emitted by the source Observable, returns an * Observable. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input * Observables being subscribed to concurrently. * @return {Observable} An Observable that emits the result of applying the * projection function (and the optional deprecated `resultSelector`) to each item * emitted by the source Observable and merging the results of the Observables * obtained from this transformation. */ function mergeMap(project, resultSelector, concurrent) { if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; } if (typeof resultSelector === 'function') { // DEPRECATED PATH return function (source) { return source.pipe(mergeMap(function (a, i) { return from_1.from(project(a, i)).pipe(map_1.map(function (b, ii) { return resultSelector(a, b, i, ii); })); }, concurrent)); }; } else if (typeof resultSelector === 'number') { concurrent = resultSelector; } return function (source) { return source.lift(new MergeMapOperator(project, concurrent)); }; } exports.mergeMap = mergeMap; var MergeMapOperator = /** @class */ (function () { function MergeMapOperator(project, concurrent) { if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; } this.project = project; this.concurrent = concurrent; } MergeMapOperator.prototype.call = function (observer, source) { return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent)); }; return MergeMapOperator; }()); exports.MergeMapOperator = MergeMapOperator; /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var MergeMapSubscriber = /** @class */ (function (_super) { __extends(MergeMapSubscriber, _super); function MergeMapSubscriber(destination, project, concurrent) { if (concurrent === void 0) { concurrent = Number.POSITIVE_INFINITY; } var _this = _super.call(this, destination) || this; _this.project = project; _this.concurrent = concurrent; _this.hasCompleted = false; _this.buffer = []; _this.active = 0; _this.index = 0; return _this; } MergeMapSubscriber.prototype._next = function (value) { if (this.active < this.concurrent) { this._tryNext(value); } else { this.buffer.push(value); } }; MergeMapSubscriber.prototype._tryNext = function (value) { var result; var index = this.index++; try { result = this.project(value, index); } catch (err) { this.destination.error(err); return; } this.active++; this._innerSub(result); }; MergeMapSubscriber.prototype._innerSub = function (ish) { var innerSubscriber = new innerSubscribe_1.SimpleInnerSubscriber(this); var destination = this.destination; destination.add(innerSubscriber); var innerSubscription = innerSubscribe_1.innerSubscribe(ish, innerSubscriber); // The returned subscription will usually be the subscriber that was // passed. However, interop subscribers will be wrapped and for // unsubscriptions to chain correctly, the wrapper needs to be added, too. if (innerSubscription !== innerSubscriber) { destination.add(innerSubscription); } }; MergeMapSubscriber.prototype._complete = function () { this.hasCompleted = true; if (this.active === 0 && this.buffer.length === 0) { this.destination.complete(); } this.unsubscribe(); }; MergeMapSubscriber.prototype.notifyNext = function (innerValue) { this.destination.next(innerValue); }; MergeMapSubscriber.prototype.notifyComplete = function () { var buffer = this.buffer; this.active--; if (buffer.length > 0) { this._next(buffer.shift()); } else if (this.active === 0 && this.hasCompleted) { this.destination.complete(); } }; return MergeMapSubscriber; }(innerSubscribe_1.SimpleOuterSubscriber)); exports.MergeMapSubscriber = MergeMapSubscriber; /** * @deprecated renamed. Use {@link mergeMap} */ exports.flatMap = mergeMap; //# sourceMappingURL=mergeMap.js.map