1 import { map } from './map';
2 import { from } from '../observable/from';
3 import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
4 export function mergeMap(project, resultSelector, concurrent = Number.POSITIVE_INFINITY) {
5 if (typeof resultSelector === 'function') {
6 return (source) => source.pipe(mergeMap((a, i) => from(project(a, i)).pipe(map((b, ii) => resultSelector(a, b, i, ii))), concurrent));
8 else if (typeof resultSelector === 'number') {
9 concurrent = resultSelector;
11 return (source) => source.lift(new MergeMapOperator(project, concurrent));
13 export class MergeMapOperator {
14 constructor(project, concurrent = Number.POSITIVE_INFINITY) {
15 this.project = project;
16 this.concurrent = concurrent;
18 call(observer, source) {
19 return source.subscribe(new MergeMapSubscriber(observer, this.project, this.concurrent));
22 export class MergeMapSubscriber extends SimpleOuterSubscriber {
23 constructor(destination, project, concurrent = Number.POSITIVE_INFINITY) {
25 this.project = project;
26 this.concurrent = concurrent;
27 this.hasCompleted = false;
33 if (this.active < this.concurrent) {
37 this.buffer.push(value);
42 const index = this.index++;
44 result = this.project(value, index);
47 this.destination.error(err);
51 this._innerSub(result);
54 const innerSubscriber = new SimpleInnerSubscriber(this);
55 const destination = this.destination;
56 destination.add(innerSubscriber);
57 const innerSubscription = innerSubscribe(ish, innerSubscriber);
58 if (innerSubscription !== innerSubscriber) {
59 destination.add(innerSubscription);
63 this.hasCompleted = true;
64 if (this.active === 0 && this.buffer.length === 0) {
65 this.destination.complete();
69 notifyNext(innerValue) {
70 this.destination.next(innerValue);
73 const buffer = this.buffer;
75 if (buffer.length > 0) {
76 this._next(buffer.shift());
78 else if (this.active === 0 && this.hasCompleted) {
79 this.destination.complete();
83 export const flatMap = mergeMap;
84 //# sourceMappingURL=mergeMap.js.map